I'm having a trouble creating Xpath for following HTML:
<html>
<body>
<table class="tablesorter">
<tbody>
<tr class="tr_class">
<td>{some td info}</td>
<td>{some td info}</td>
<td>
<span class="span1">
<span class="span2">Out</span>
<span class="span3">SMTH</span>
<span class="span4">Out</span>
</span>
</td>
</tr>
<tr class="tr_class">
<td>{some td info}</td>
<td>{some td info}</td>
<td>In</td>
</tr>
<tr class="tr_class">
<td>{some td info}</td>
<td>{some td info}</td>
<td>In</td>
</tr>
</tbody>
</table>
</body>
</html>
What I want is to create Xpath that will return me content of each third td node (if it doesn't have children) or a content of it's span that has class="span2".
For example, for this html it should return
Out,In,In
I have Xpath that will return needed span node, it looks like:
//table[#class = 'tablesorter']//td[3]/descendant::*[#class='span2']/text()
and I have Xpath that will return me simple content of each 3d td nodes:
//table[#class = 'tablesorter']//td[3][count(descendant::*)=0]/text()
But I need only one Xpath, because for me it is necessary to have a right ordering of 'In' or 'Out' values (their ordering in the table)
This will do it, no idea how robust it will be for your "corpus":
//table[#class="tablesorter"]/tbody/tr/td[3]/descendant::text()[normalize-space(.)!=""]
['Out', 'In', 'In']
Update
//table[#class="tablesorter"]/tbody/tr/td[3]/descendant::text()[normalize-space(.)!=""][parent::td or parent::span[#class="span2"]]
Related
I need to build an xpath that returns my element only if dependency is present on the screen.
I'm currently filtering my element using the following snippet:
//div[contains(text(), 'my element')]
...
<div>
<table>
<tbody>
<tr>
<td>
<a>dependency</a>
</td>
</tr>
</tbody>
</table>
</div>
<div>
<div>my element</div>
</div>
//div[contains(text(), 'my element')][//a[contains(text(), 'dependency')]]
I have a table that looks like this:
<table>
<thead>
<tr>
<th data-th-key="col1"> <span> Foo </span> </th>
<th data-th-key="col2"> <span> Bar </span> </th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<input data-model-key="col1">
</div>
</td>
<td>
<div>
<input data-model-key="col2">
</div>
</td>
</tr>
</tbody>
</table>
To find the right input element I have to know the data-th-key from the table head. Is there any way to use the first xpath inside the second one?
Xpath one:
//table//thead//span[translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='foo']/parent::th/#data-th-key"
Xpath two:
//table//tbody//tr//td//div//input[#data-model-key='col1']
So I want to replace the col1 value in the second one with Xpath one.
You can write an expression like
//table//tbody//tr//td//div//input[#data-model-key = //table//thead//span[translate(normalize-space(.),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='foo']/parent::th/#data-th-key]
yes, that is possible and meaningful and selects the input (or the inputs) whose data-model-key is equal to that data-th-key attribute.
In this example i have this code:
<table class="basicinfo" cellspacing="0">
<tr class="header">
<td colspan="3">
<div>
<h2 class="prod_card">Basic info</h2>
</div>
</td>
</tr>
<tr class="row2 item">
<td class="cell0">
<div>
Year
</div>
</td>
<td class="cell1">
<div>
2005
</div>
</td>
</tr>
<tr class="row3 item alt">
<td class="cell0">
<div>
Extra
</div>
</td>
<td class="cell1">
<div>
-
</div>
</td>
</tr>
</table>
Now, i want to get (for example) the year. I'm trying to get the next div content after the div with Year content.
I'm using this xpath without success:
//div[preceding-sibling::div = 'Year']
And anyone knows a good website to start learning xpath? Thanks in advance!
Following XPath
//div[parent::td/preceding-sibling::td//div[normalize-space()= 'Year']]
has the result
<div>2005</div>
Your XPath didn't work because the div you are looking for has not a div element as preceding-sibling but a td as parent. The preceding-sibling::td of this td contains a div which has the text "Year" - td//div. Using normalize-space() returns the text of this div without any spaces which can be necessary when checking for equality.
For reference: normalize-space(), and as you asked for good resources - I don't want to recommend anything special, but you should have a look at the resources mentioned at the info given on stackoverflow here: https://stackoverflow.com/tags/xpath/info (if not already done) and, for XPath axes, this is a good visualization: http://www.xmlplease.com/axis
I have similiar structure to:
<tr>
<td>I WANT THIS</td>
<td>
<a class="unread">text</a>
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<a class="read">text</a>
</td>
<td></td>
</tr>
And I need to select <tr> node, which have <a> node with attribute [#class='unread'], to select inner <td> later.
I tried //tr[a/#class='unread'] and //tr/a[#class='textMsg unread'] but didn't work. How can I get my <tr> node?
a tag is not a child of tr tag, you can try this xpath:
//tr[.//a/#class='unread']
Or
//tr[descendant::a/#class='unread']
To select the wanted td element(s), use:
//tr//td[.//a[#class = 'unread']]
If it is known that the td is a child of the tr and the a is a child of the td, the above may be simplified to:
//tr/td[a[#class = 'unread']]
I need to detemine xpath for element mainForm:queryConfigure:fetchReport.
<span id="mainForm:queryConfigure:j_id18">
<table id="mainForm:queryConfigure:j_id19"
class="showReportTable" align="center">
<tbody>
<tr>
<td>
<input id="mainForm:queryConfigure:fetchReport" type="image"
src="images/show_report.gif" name="mainForm:queryConfigure:fetchReport"/>
</td>
</tr>
</tbody>
</table>
</span>
I tried
selenium.click("//input[#id='mainForm:queryConfigure:fetchReport'][#type='image'][#src='images/show_report.gif']");
and
selenium.click("//input[#id='mainForm:queryConfigure:fetchReport']");
One more case:
<div class="tabUnselectedText" align="center">
Notifications
</div>
Id and name attribute values are acceptable locators for method click. See locating elements in the documentation.
selenium.click('mainForm:queryConfigure:fetchReport');