Home > Back-end >  Why getting selenium.common.exceptions.NoSuchElementException in selenium python Script?
Why getting selenium.common.exceptions.NoSuchElementException in selenium python Script?

Time:11-23

I am new to selenium and written a script to login, and when I am running the script getting the following error:-

Error:-

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="SignIn Form_email"]"}

Script:-

url = "https://someurl.com"
driver = webdriver.Chrome(executable_path="C:\CustomDrivers\chromedriver_win32\chromedriver")
driver.get(url)
print(driver.title)
user_id = driver.find_element_by_id("SignIn Form_email")
user_id.send_keys("[email protected]")

user_password = driver.find_element_by_id("SignIn Form_password")
user_password.send_keys("web$$DD")

driver.find_element_by_xpath("//*[@id='SignIn Form']/div[3]/div/div/div/button").click()
print(driver.title)
time.sleep(5)

Full Error Trace:-

C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path="C:\CustomDrivers\chromedriver_win32\chromedriver")
Convin
C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py:9: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  user_id = driver.find_element_by_id("SignIn Form_email")
Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\convin\automation_test\logintest.py", line 9, in <module>
    user_id = driver.find_element_by_id("SignIn Form_email")
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 466, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1238, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Lenovo\Desktop\convin\automation_test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, 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":"[id="SignIn Form_email"]"}
  (Session info: chrome=95.0.4638.69)
Stacktrace:
Backtrace:
    Ordinal0 [0x00780C43 2493507]
    Ordinal0 [0x0071A4B1 2073777]
    Ordinal0 [0x00622608 1058312]
    Ordinal0 [0x0064CAA4 1231524]
    Ordinal0 [0x00676C62 1404002]
    Ordinal0 [0x0066597A 1333626]
    Ordinal0 [0x00675038 1396792]
    Ordinal0 [0x0066580B 1333259]
    Ordinal0 [0x00642314 1188628]
    Ordinal0 [0x0064316F 1192303]
    GetHandleVerifier [0x00907BF6 1548950]
    GetHandleVerifier [0x009B461C 2256060]
    GetHandleVerifier [0x0080C13B 518107]
    GetHandleVerifier [0x0080B1E0 514176]
    Ordinal0 [0x0071F53D 2094397]
    Ordinal0 [0x00723418 2110488]
    Ordinal0 [0x00723552 2110802]
    Ordinal0 [0x0072CE81 2150017]
    BaseThreadInitThunk [0x7602FA29 25]
    RtlGetAppContainerNamedObjectPath [0x77467A9E 286]
    RtlGetAppContainerNamedObjectPath [0x77467A6E 238]

Thanks in advance Hope to here from you soon.

CodePudding user response:

Where is the HTML? Without it, we can't really help you.

By the way, your user_id and user_password look like a class name, are you sure about find_element_by_id method? Shouldn't you use find_element_by_class instead?

CodePudding user response:

By seeing this error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="SignIn Form_email"]"}

You will have to make sure

  1. This [id="SignIn Form_email"] is not under an iframe.
  2. This [id="SignIn Form_email"] is not under a shadow-root.
  3. You should not be on the new tab/windows launched by selenium.
  4. You may be trying to interact with the element and it has not rendedered completely.

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

xpath that you should check :

[id="SignIn Form_email"]

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.

Now, if it is iframe, please switch to iframe first.

wait = WebDriverWait(driver, 30)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "")))

if it is element not rendered issue, Please induce web driver wait like

wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "")))

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • Related