0
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??

3
  • Share the URL. If not possible, update the question with HTML. Commented Sep 30, 2021 at 4:00
  • Sounds like that's in iframe, please check for iframe. Commented Sep 30, 2021 at 4:59
  • no iframe or what so ever it was just wrapped inside a form tag and a fieldset tag Commented Sep 30, 2021 at 12:59

2 Answers 2

1

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 right locator

  • 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 not visible when you try to perform action.

  • From error message I can see you are using css selector #loginUsername. I assume you are not giving tag name like it suppose to be input#loginUsername

Sign up to request clarification or add additional context in comments.

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.