Issue
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 expression from the browser. I thought maybe I needed to wait for the page to load, so I added a 30 second timer, but it still couldn't find the element.
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:
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
The id that you are using with your XPath expression, ctl00_cphMain_RadDock1455_C_ctl00_gridTimesheets_ctl00__0'
, could be dynamic in nature, which means at 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 the anchor tag.
driver.find_element(By.LINK_TEXT, "Data Analyst").click()
in general should work. However, if you are looking for a 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 in all, you've to make sure that it is unique in nature.
Steps to check:
Press F12 in Chrome → go to the element section → do a Ctrl + F →. Then paste the XPath expression and see if your desired element is getting highlighted with 1/1 matching node.
NoSuchElementException:
Please check in the Web development tools (Google Chrome) if we have unique entry in the HTML DOM or not.
The XPath expression 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 expression 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 the 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.querySelector
to have returned a web element and then operates accordingly.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)
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 window 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.
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 the default content and then switch to the respective iframe.
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.