After opening up a webpage, webdriver can't find the "name" element?
Traceback (most recent call last):
File "main.py", line 19, in <module>
driver.find_element(By.NAME,"nickname").send_keys(username Keys.ENTER)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="nickname"]"}
(Session info: chrome=)
Stacktrace:
#0 0x5574d3a05919 <unknown>
Here's the HTML source I used:
<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" autocomplete="off" value="" aria-expanded="false">
CodePudding user response:
Aan <inpt>
element is a clickable element and ideally to locate any clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname[name='nickname'][placeholder='Nickname'][data-functional-selector='username-input']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='nickname' and @name='nickname'][@placeholder='Nickname' and @data-functional-selector='username-input']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on NoSuchElementException in:
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
CodePudding user response:
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
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 //input[@id='nickname' and @name='nickname']
then you need to check for the below conditions as well.
- Check if it's in any
iframe/frame/frameset
. - Check if it's in any
shadow-root
. - Make sure that the element is rendered properly before interacting with it. Put some
hardcoded delay
orExplicit wait
and try again. - If you have redirected to a
new tab/ or new windows
and you have not switched to that particularnew tab/new window
, otherwise you will likely getNoSuchElement
exception. - 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.
to switch to frame:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "frame XPath here")))
to switch to default content:
driver.switch_to.default_content()
If you're using explicit wait, then you would need below imports as well.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC