简体   繁体   中英

Can't find element - xPath correct

So, I have an XPath (I've verified this works and has 1 unique value via Google Chrome Tools.

I've tried various methods to try and get this xpath, initally using right click > copy xpath in chrome gave me:

//*[@id="hdr_f0f7cdb71b9a3f44782b87386e4bcb3e"]/th[2]/span/a

However, this ID changes on every reload.

So, i eventually got it down to:

//th[@name="name"]/span/a/text()

element = driver.find_element_by_xpath("//th[@name='name']/span/a/text()")
print(element)


selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//th[@name='name']/span/a/text()"}

check this:

len(driver.find_elements_by_xpath('//*[contains(@id, "hdr_")]'))

if you won't get a lot of elements you're done with this:

driver.find_elements_by_xpath('//*[contains(@id, "hdr_")]')

You should not be using /text() for a WebElement. Use "//th[@name='name']/span/a" as the Xpath and print the text using element.text (Not sure about the exact method for Python, but in Java it is element.getText() )

I will suggest using absolute XPath rather than relative XPath it might resolve it if the id is changing with every load. please see below how absolute XPath is different from relative using the google search bar.

  • Relative XPath - //*[@id="tsf"]/div[2]/div/div[1]/div/div[1]/input
  • absolute XPath - /html/body/div/div[3]/form/div[2]/div/div[1]/div/div[1]/input

I understand as you said you cannot share a link but people here can help if you can share inspect element snapshot showing DOM. so that if there is an issue in XPath it can be rectified. Thanks :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM