hbae
hbae

Reputation: 89

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?

Upvotes: 0

Views: 861

Answers (1)

hfontanez
hfontanez

Reputation: 6168

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']

Upvotes: 1

Related Questions