'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"}
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.
Check if it's in any
iframe/frame/frameset.Solution: switch to iframe/frame/frameset first and then interact with this web element.
Check if it's in any
shadow-root.Solution: Use
driver.execute_script('return document.querySelectorto have returned a web element and then operates accordingly.Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delayorExplicit waitand try again.Solution:
time.sleep(5)orWebDriverWait(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")If you have redirected to a
new tab/ or new windowsand you have not switched to that particularnew tab/new window, otherwise you will likely getNoSuchElementexception.Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first
switch to default contentand 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 |

