Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select element with changing xpath

Example Image

say I wanted to select the element with class=kbkey button red_selected sel. Its xpath from the example in the pic would be //*[@id="virtualKeysWrapper"]/div[3], so I have the following code:

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="virtualKeysWrapper"]/div[3]'))).click()

However, the div position of this element would change everytime I refresh the site. Was wondering what should I do to successfully select the element with class=kbkey button red_selected sel successfully everytime?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Avoid using index position in XPath if at all possible for this very reason. Without knowing exactly what the rest of the DOM looks like, my best guess is that you could use the following expression:

//div[@id='virtualKeysWrapper']/div[@class='kbkey button red_selected sel']

Alternatively, you could use

//div[@id='virtualKeysWrapper']/div[@sel='true']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading