Home > database >  how to set an xpath like this to use in selenium python
how to set an xpath like this to use in selenium python

Time:08-28

i have a xpath like this that it works with selenium python

//*[@id="POPOVER41"]/div/div[1]/div/div/div[3]/div[2]/div/div/div/div/div

link = driver.find_element(By.XPATH, '//*[@id="POPOVER41"]/div/div[1]/div/div/div[3]/div[2]/div/div/div/div/div]')

the problem is that the numbers 41 in this example, @id="POPOVER41" can change to be any other numbers, 1 or 2 or 3 numbers like @id="POPOVER4" or @id="POPOVER33" or @id="POPOVER541" is there a way to set an xpath expression that fits this change i mean after POPOVER just check if you have any numbers and be able to scrape that element in the web page with python selenium even if those numbers change on page reload thanks a lot

CodePudding user response:

I've had this problem before, and the only way to solve this is by using other properties from other web elements that won't change that will enable you to identify the one that you're looking.

Unfortunately, you can't use the id property because it will change every time you reload the page.

CodePudding user response:

//*[@id="POPOVER41"]

could be replaced with to allow for a dynamic id

//*[starts-with(@id, 'POPOVER')]
  • Related