David Seroy
David Seroy

Reputation: 199

Dynamic and changing XPATH

I am writing a script to navigate a web page using selenium. I usually just find and select all elements based on XPATH. However, one of the elements xpath's appears to be changing slightly every week or so. How can I select or find this element without being subject to the dynamic XPATH?

Here is how I currently select element:

driver.find_element_by_xpath('//*[@id="footable_213701"]/tfoot/tr/td/div/ul/li[15]/a')

Here is the element and attributes

<li class="footable-page-nav" data-page="next">
<a class="footable-page-link" href="#">></a>
</li>

I'm needing to select the ">" button to navigate to next page. I cannot select by class="footable-page-link" because there are a dynamic number of those so I don't know which one to select.

Upvotes: 0

Views: 400

Answers (2)

Bhargavi
Bhargavi

Reputation: 13

Best way to write XPATH is to avoid using numeric values (i.e id's with numeric values) which means they are dynamic in nature and li[15] here you are referencing the 15th list item which may change due to addition or deletion of an item, which will fail your XPATH.

try below,

driver.findElement(By.XPATH("//li[@class='footable-page-nav']/a"));

Thank You,

Upvotes: 0

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

There are couple of ways you can try it in xpath,

1) Click on the anchor based on data-page - Next

//li[@data-page="next"]/a

2) Click on '>' using that as a text,

//li[@class="footable-page-nav"]/a[contains(.,'>')]

or simply,

//a[contains(.,'>')]

Upvotes: 2

Related Questions