Reputation: 58
I have an automated python script to check for any changes in the DOM. I have a xpath that works:
//td[@class='high-bg']/a[@class='link-action'][@data-hintbox='1'][@data-hintbox-static='1'][@role='button'][@href='javascript:void(0)']
But it gives me more outputs than I need and some of them cause errors. So i want to get the abstract items that i need from an xpath, so I'm trying to use something like this:
//table[@id't5c711109b1eea263276674']/tbody[]/tr[]/td[@class='warning-bg']/a[@class='link-action'][@data-hintbox='1'][@data-hintbox-static='1'][@role='button'][@href='javascript:void(0)']
But it's not working, so is it even possible to search xpath with that many tags?
<table id="example">
<tbody>
<tr>
<td class="average-bg">
<a class="link-action" data-hintbox="1" data-hintbox-static="1" role="button" href="javascript:void(0)">1</a>
</td>
</tr>
</tbody>
</table>
Upvotes: 2
Views: 3102
Reputation: 193048
About your code trials:
In your first attempt you have tried to use all the attributes of the element to construct the xpath:
//td[@class='high-bg']/a[@class='link-action'][@data-hintbox='1'][@data-hintbox-static='1'][@role='button'][@href='javascript:void(0)']
Elements with similar attributes can be present but at different position/location. Hence it returns more than desired outputs you need.
In your second attempt you have constructed an absolute xpath which is brittle:
//table[@id't5c711109b1eea263276674']/tbody[]/tr[]/td[@class='warning-bg']/a[@class='link-action'][@data-hintbox='1'][@data-hintbox-static='1'][@role='button'][@href='javascript:void(0)']
As per the text based HTML you have provided, to identify the desired element you can use either of the following solutions:
xpath
:
driver.find_element_by_xpath("//table[@id='example']//td[@class='average-bg']/a[@class='link-action' and text()='1']")
css_selector
:
driver.find_element_by_css_selector("table#example td.average-bg>a.link-action")
Upvotes: 3
Reputation: 23815
Here it is.
XML
<root>
<e1 role='a' class='y'></e1>
<e1 role='a' class='t'></e1>
<e1 role='a' class='z'></e1>
</root>
XAPTH
//e1[@role='a' and @class='t']
Output
Element='<e1 class="t" role="a"/>'
Upvotes: 0