Issue
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" class="sc-gTgzIj eFnEAY" autocomplete="off" value="" aria-expanded="false">
Solution
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
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.