How to select element from a list and go to parent and then sibling (by Xpath)
How to select element from a list and go to parent and then sibling (by Xpath)
I am trying to find a way to press button that is in the same tr as text with which I can localize specific file that I'd like to download. I'll describe it in details:
there is a website that contains many tr. Each tr contains couple child td. These td contain description and download button. I need to localize desired tr by description of its td. Then I'd like to go to td that contains download button for this tr.
Descriptions are different but I am interested only in those that contain 'DV' text. There is always couple of them on a website but I'd like to select first. What i've figured out so far is:
$x("//*[contains(text(), 'DV')]")[0]
And then I have no idea how to go to parent element and then to sibling td that contains download button.
HTML looks something like that
<tr>
<td class='a'>DV some other characters1</td><td class='a'>Download button code</td>
</tr>
<tr>
<td class='a'>DV some other characters2</td><td class='a'>Download button code</td>
</tr>
<tr>
<td class='a'>Some other description</td><td class='a'>Download button code</td>
</tr>
As I mentioned above- I'd like to get the first td with DV and then go to download button.
4 Answers
4
As per the HTML you have shared to locate the first <td>
that contains the text DV and identify the corresponding element containing the text Download you can use the following xpath:
<td>
"//tr//td[@class='a'][contains(.,'DV')]//following::td[1]"
Try below XPath to locate first "Download" button in the same row with td
that starts with "DV"
td
"DV"
(//tr[starts-with(td, 'DV')]/td[.='Download button code'])[1]
or with following-sibling
:
following-sibling
(//td[starts-with(., 'DV')]/following-sibling::td[.='Download button code'])[1]
Always something new.
– SIM
Jul 2 at 19:19
You can do it like this:
//td/parent::tr - gives the parent 'tr' of 'td'
the exactly xPath
I cannot provide, while not exactly understand what you want to achive. But it is not hard to find using my example. You can also find more information about xPath axes here.
xPath
You can refer this:
//td[contains(text(),'characters1')]//following::td[contains(text(),'Download button code')]
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Bingo! Works just fine. Big thank you!
– ugabuga77
Jul 2 at 10:55