Home > Net >  select element with changing xpath
select element with changing xpath

Time:11-27

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?

CodePudding user response:

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