How to get xpath for this element - selenium

I have lot of tr tag with same structure in my page . The only way to select "Google" is by starting the XPATH from "ImgTrash.png" . Please let me know if you have idea .
<tr>
<td width="1%"/>
<td class="Fol" nowrap="" style="padding-left: 0px">
<a id="A57" href="3745474">
<img src="ImgTrash.png"/>
<span style="vertical-align:middle">Trash </span>
</a>
</td>
</tr>
<tr class="Row">
<td width="1%"/>
<td class="Fol" nowrap="" style="padding-left: 8px">
<a id="A56" href="507023">
<img src="ImgFolder.png"/>
<span style="vertical-align:middle">Google </span>
</a>
</td>
</tr>

You will need an XPath like this:
//a[img/#src = 'ImgFolder.png']/span
Breakdown:
//a searches for all <a> elements
[img/#src = 'ImgFolder.png'] only selects the <a> elements that have a child <img> with the attribute src. This attribute src should exactly match (case-sensitive) ImgFolder.png
/span at the end will get the child <span> from the previous selected <a>

Related

Get td class text using VBA Selenium

So I want to take the text of td class.
The html page:
<table class="track-summary-details__info col-table ng-scope" ng-repeat="checkpoint in checkpointGroupCtrl.group.checkpoints">
<tbody><tr class="track-summary-details__row ng-isolate-scope" ewf-checkpoint="checkpoint">
<td class="track-summary-details__counter ng-binding" ng-bind="checkpointCtrl.item.counter">9</td>
<td class="track-summary-details__status ng-binding" ng-bind="checkpointCtrl.item.description">Delivered</td>
<td class="track-summary-details__location ng-binding" ng-bind="checkpointCtrl.item.location">EAST - UK</td>
<td class="track-summary-details__time ng-binding" ng-bind="checkpointCtrl.item.time">10:46am</td>
<td class="track-summary-details__pieces ng-isolate-scope" ewf-pieces="checkpointCtrl.item.pieces">
<!-- ngIf: piecesCtrl.pieces.pieceIds.length --><div ng-if="piecesCtrl.pieces.pieceIds.length" class="ng-scope">
<a data-tracking="{'type':'piecesItem','title':{'en': 'Toggle the pieces'}}" ng-click="piecesCtrl.togglePieces()" aqa-id="trackingResultsPieces">
<i ng-class="piecesCtrl.arrowClass" class="dhlicon-carat-down"></i>
<span ng-bind="piecesCtrl.pieces.amount" class="ng-binding">1</span>
<span ng-bind="piecesCtrl.pieces.label" class="ng-binding">Piece</span>
</a>
<!-- ngIf: piecesCtrl.isPiecesVisible -->
</div><!-- end ngIf: piecesCtrl.pieces.pieceIds.length -->
</td>
</tr>
</tbody></table>
The text I want to take is "Delivered".
I tried using the xpath by copying it but the format is very strange :
/html/body/div[2]/div/div/div/div/div/div/div[2]/div[2]/div/div/div/div1/div/div[2]/div[2]/table1/tbody/tr/td[2]
So, I have tried this xpath :
Set elem = Selenium.FindElementByXPath("//td[#class='track-summary-details__status ng-binding']")
Seems to be found because no error but the result is not good :
result
Do you have any idea to get the value ?
Thanks in advance

How to ignore that clicking on a TD tag does not automatically redirect me to another view? Vue

I have a table where in each TR when I click it redirects me to another view with the detail, the idea is that I have never worked with Vue and I can't think how to disable the event when I click on the first TD tag of the table.
Before in Jquery I did it this way:
//Add onclick to TR
$('table tr').click(function(e) {
// Avoid redirecting if it's a delete button
if(!$(e.currentTarget).hasClass('delete')) {
// Not a button, redirect by taking the attribute from data-route
window.location.href = $(e.currentTarget).data('route');
}
});
But with Vue I don't know how to do it in the onclick event.
This is my table and the method
<table
id="accounts-table"
class="
table table-listbox
table-bordered_
table-responsive-md
table-striped_
text-center_
"
>
<thead>
</thead>
<tbody>
<tr v-if="tableData.length === 0">
<td colspan="4" class="text-center">
No hay oportunidades para mostrar.
</td>
</tr>
<template v-if="loading === true">
<tr colspan="9" class="text-center">
<b-spinner variant="primary" label="Spinning"></b-spinner>
<b-spinner variant="primary" type="grow" label="Spinning"></b-spinner>
</tr>
</template>
<template v-else>
<tr
v-for="(data, k) in tableData"
:key="k"
#click="viewOpportunity(k, data)"
>
<slot :row="data">
<td v-if="permissions.includes('view all opportunities')" width="10">
<div class="iq-email-sender-info">
<div class="iq-checkbox-mail">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="mailk">
<label class="custom-control-label" for="mailk"></label>
</div>
</div>
</div>
</td>
<td width="20">
<vue-avatar
:username="data.sales_man"
:title="data.sales_man"
:size="32"
v-if="data.sales_man != ''"
></vue-avatar>
</td>
<td width="5">
<template v-if="data.has_file != false">
<i
class="ri-attachment-line"
style="font-size: 20px; color: #007bff"
></i>
</template>
</td>
<td width="120" nowrap>
<template>
<p class="mb-0">{{ data.created_at }}</p>
<small class="text-info mt-5">
Fecha creación
</small>
</template>
</td>
</slot>
</tr>
</template>
</tbody>
</table>
viewOpportunity(data) {
window.location.href = "/opportunity/" + data.id;
},
For example in the first TD of the table I would like that it does not redirect me to the page but from the 2nd TD it redirects me.
On first td element use this even modifier - #click.stop
Prevent on click on parent when clicking button inside div
This kind of issue ever answered .. please have a look on this
If someone needs an href inside a table that has a that has a #click method in the row, but you wat to the link does not fire the click event in the row and also to prevent event propagation when there is no method to call, e.g. a simple <a href="mailto:someone#example.org"> link, try to use this trick with #click.self:
BEFORE
<table>
<tr>
<th>COLUMN A</th>
<th>COLUMN B</th>
</tr>
<tr #click="myMethodFromRow">
<td>Some Value</td>
<td>
<a target="_blank" :href="mailto:someone#example.org"> My link </a>
</td>
</tr>
</table>
AFTER
<table>
<tr>
<th>COLUMN A</th>
<th>COLUMN B</th>
</tr>
<tr>
<td #click="myMethodFromRow">Some Value</td>
<td #click.self="myMethodFromRow">
<a target="_blank" :href="mailto:someone#example.org"> My link </a>
</td>
</tr>
</table>
In this case I wanted to execute myMethodFromRow clicking inside each td, but not clicking the link. So I changed the myMethodFromRow method from the tr and put it into each td. But the most important trik was to use #click.self for the td with the href link.
.self only trigger handler if event.target is the element itself, i.e. not from a child element.
DOCUMENTATION: https://vuejs.org/guide/essentials/event-handling.html#event-modifiers

Unable to locate element using xpath

Am very new to Selenium just learnt few things and trying to automate.Unable to locate an element in Selenium webdriver for the below one:
<div class="navBg withSubMenu">
<table id="topnav" class="navTable" cellspacing="0" cellpadding="0" style="-moz-user-select: none; cursor: default;">
<tbody>
<tr>
<td class="logoCell navCell" valign="top">
<td class="navItem navCell relative selected">
<td class="navItem navCell relative notSelected">
<a class="content tasks" href="/tasks/otasklist.do">
<div class="label" style="z-index:155; ">TASKS</div>
<div class="img"> </div>
</a>
</td>
<td class="navItem navCell relative notSelected">
<a class="content reports" href="/reports/reports.do">
</td>
<td class="navItem navCell relative notSelected">
<td class="menuCell navCell" valign="top">
</tr>
<tr class="secondLevelRow">
</tbody>
</table>
I have written code like
driver1.findElement(By.xpath("//*[Contains(#class,'content tasks')]")).click();
Anyone please help me.
And also please suggest some sites or links to learn more about locators especially xpath. I tried few but not getting it in depth.
Thanks in advance.
Start contains with a lower case c:
driver1.findElement(By.xpath("//*[contains(#class, 'content tasks')]")).click();
<a class="content tasks" href="/tasks/otasklist.do">
driver1.findElement(By.xpath("//a[#class='content tasks']")).click();

Locate text in a page by using other text in xpath

How can I locate some text in a span in one row using other text that's in other span in other row by xpath in selenium, I don't want to traverse by html tags as html template can be changed dynamically and also I don't have a unique Id.
Below is my code with highlighted text as question and answer text :-
<div id="surveyquescontent">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<span class="mrQuestionText" style="">**Have you chosen your own date for when your electric bill is due?**</span>
</td>
</tr>
<tr>
<td>
<span style="">
<span class="mrQuestionTable" style="display:block;margin-left: 1em;">
<span id="Cell.0.0" style="">
<label for="_Q1_C0">
<span class="mrSingleText" style="">**Yes**</span>
</label>
</span>
</span>
</span>
</td>
</tr>
</tbody>
</table>
</div>
You can try to select the correct <tr> and use following-sibling::tr and continue digging to the desired <span> for example :
//tr[.//span[#class='mrQuestionText']]
/following-sibling::tr[1]
//span[#class='mrSingleText']
In above example I'm using class attribute value to get correct <tr>, you can change it easily to be using text within the <span> if you have to, or using both text and class :
//tr[.//span[
#class='mrQuestionText'
and
.='Have you chosen your own date for when your electric bill is due?'
]]
/following-sibling::tr[1]
//span[#class='mrSingleText']

Selenium inside siblings

Is there a way to click further inside siblings in Selenium Webdriver? For example, here's the HTML:
<tr class="rich-table-row xx-datalist-even" onmouseover="Element.addClassName(this, 'xx-datalist-mouseover')" onmouseout="Element.removeClassName(this, 'xx-datalist-mouseover')">
<td class="rich-table-cell xx-datalist " id="userTableForm01:usersTableData:264:j_id102" style="text-align:center; width:20px;">
<img src="/static/images/graphics/status1.gif" title="Disabled" />
</td>
<td id="userTableForm01:usersTableData:264:col1" class="rich-table-cell xx-datalist">
<span id="userTableForm01:usersTableData:264:author_id">2377</span>
</td>
<td id="userTableForm01:usersTableData:264:col2" class="rich-table-cell xx-datalist">seleniumtest2312</td>
<td id="userTableForm01:usersTableData:264:col3" class="rich-table-cell xx-datalist">
<span style="margin-right:3px">Test2312</span>
Selenium
</td>
<td class="rich-table-cell xx-datalist " id="userTableForm01:usersTableData:264:col4" style="text-align:center;">
<input type="checkbox" name="userTableForm01:usersTableData:264:j_id111" onclick="A4J.AJAX.Submit('userTableForm01',event,{'similarityGroupingId':'userTableForm01:usersTableData:264:j_id112','parameters':{'userTableForm01:usersTableData:264:j_id112':'userTableForm01:usersTableData:264:j_id112'} } )" />
</td>
<td id="userTableForm01:usersTableData:264:col6" class="rich-table-cell xx-datalist">
<a id="userTableForm01:usersTableData:264:enable_link" href="#" style="margin-right:5px" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:enable_link':'userTableForm01:usersTableData:264:enable_link'},'');}return false">
<span id="userTableForm01:usersTableData:264:enable_link_text" class="xx-datalist">Enable</span>
</a>
<a id="userTableForm01:usersTableData:264:edit_link" href="#" style="margin-right:5px" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:edit_link':'userTableForm01:usersTableData:264:edit_link'},'');}return false">
<span id="userTableForm01:usersTableData:264:edit_link_text" class="xx-datalist">Edit</span>
</a>
<a id="userTableForm01:usersTableData:264:remove_link" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:remove_link':'userTableForm01:usersTableData:264:remove_link'},'');}return false">
<span id="userTableForm01:usersTableData:264:remove_link_text" class="xx-datalist">Delete</span>
</a>
</td>
</tr>
Now, this is a much bigger table than shown, so there are a lot more tr's with more users. The ID's are all auto-generated and different each time, and I am trying to click the 'Enable'-link, though the only thing unique on the same row that I can use is the username seleniumtest2312, which has brought me to this:
driver.findElement(By.xpath("//td[text()='seleniumtest2312']/following-sibling::td[3]/span[text()='Enable']")).click();
But it just won't work (no such element).
If there are better ways of solving this issue I'll be happy to try them.
The last part of your XPath is a bit off :
following-sibling::td[3]/span[text()='Enable']
because <span> you're looking for isn't direct child of <td>. You can try this XPath instead :
//td[.='seleniumtest2312']/following-sibling::td[3]/a/span[.='Enable']