Home > OS >  How to handle dynamically changing value with selenium and python?
How to handle dynamically changing value with selenium and python?

Time:09-28

Below is what HTML has. If I click using below XPATH, it's working fine. But data-qtip value keep changing and also there are other class with same name as above in HTML.

How can I click on whatever the value that appears @data-qtip?

HTML:

<a href="javascript:void(0)">    
<span class="item-context-link" data-qtip="3711672330">3711672330</span>    
</a>

Code:

driver.find_element_by_xpath("//span[contains(@data-qtip, '3711672330')]").click()

CodePudding user response:

In your DOM if data-qtip is unique attribute then you can use below xPath

xPath:

//span[@data-qtip]

Code:

driver.find_element_by_xpath("//span[@data-qtip]").click()

CodePudding user response:

based on OP response, there are 9 data-qtip and due to lack of HTML, I will proide you solution using xpath indexing.

(//span[@data-qtip])[1]

should represent the first 1 element, [2] should represent the second element.

(//span[@data-qtip])[2]

and so on..

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

You can try to increase the index to have a unique matching node in HTMLDOM.

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("(//span[@data-qtip])[1]").click() 
  • Related