Home > Mobile >  Why can't selenium find XPath even though I copied it from the browser?
Why can't selenium find XPath even though I copied it from the browser?

Time:04-28

I'm having a hard time clicking a particular link. I tried using find_element_by_css_selector and find_element_by_xpath. Both give me the following error, even though I copied the xpath from the browser. I thought maybe I needed to wait for the page to load so I added a 30 second timer and it still can't find the element. Any help would be greatly appreciated thanks.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0']/td[2]/a"}

HTML: enter image description here

Code:

time.sleep(30)

link = driver.find_element_by_xpath("//*[@id='ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0']/td[2]/a")
link.click()

CodePudding user response:

Try:

time.sleep(30)

link = driver.find_element_by_xpath("//a[@title='Data Analyst']")
link.click()

CodePudding user response:

The id that you are using with your xpath ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0' could be dynamic in nature, which means at the run time some value may change and you may get NoSuchElement exception

You can use link_text or partial_link_text in case Data Analyst is unique text wrapped inside anchor tag.

driver.find_element(By.LINK_TEXT, "Data Analyst").click()

in general should work, however if you are looking for reliable solution, you should go for explicit waits:

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Data Analyst"))).click()

You'd have to import below:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

In case you are looking for xpath-based locator, you can use

//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']

All you've to make sure that it should be unique in nature.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

  • Related