raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #loginUsername
I used selenium module to automate the login process of some websites and when I executed the program, it throws NoSuchElementException
while I tried all the find_element_by_*
methods ( by id, by class, by css selector, by text, by name attr ) but, still I get this error and I don't know how to make this program error-free so, is there any other way to debug the program??
CodePudding user response:
I don't have enough reputation to comment so posting an answer. I assume any one of the below reason can be the cause.
Please check the element
xPath
id
etc. in chrome console to make sure you are using rightlocator
There is not enough time you are giving for element to be accessible. Please use
explicit
wait.May be element is
present
on DOM but notvisible
when you try to perform action.From error message I can see you are using
css
selector#loginUsername
. I assume you are not givingtag
name like it suppose to beinput#loginUsername
CodePudding user response:
I understand that there are no iframes from what you said above.
Make sure that you wait for the element to be loaded and made available before you call find_element(...).
Do something like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("D:/chromedriver/94/chromedriver.exe")
driver.get("https://yourwebsite.com")
# use By.XPATH, By.CLASS_NAME etc. depending on the
# element you're referring to
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, 'loginUsername')))
login = driver.find_element(By.ID, 'loginUsername')