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

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()


Solution 1:[1]

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.

Update:

NoSuchElementException:

Please check in the dev tools (Google chrome) if we have unique entry in HTML-DOM or not.

xpath that you should check :

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

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.

If this is unique //tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst'] then you need to check for the below conditions as well.

  1. Check if it's in any iframe/frame/frameset.

    Solution: switch to iframe/frame/frameset first and then interact with this web element.

  2. Check if it's in any shadow-root.

    Solution: Use driver.execute_script('return document.querySelector to have returned a web element and then operates accordingly.

  3. Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.

    Solution: time.sleep(5) or

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//tr[@class='rgRow']//td[2]/a[@title='Data Analyst' and text()='Data Analyst']"))).send_keys("test")

  4. If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.

    Solution: switch to the relevant window/tab first.

  5. If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.

    Solution: switch to default content and then switch to respective iframe.

Solution 2:[2]

Try:

time.sleep(30)

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Software Tester at ExpandCart