Home > OS >  Python Selenium error: Unable to locate the Element by Name:
Python Selenium error: Unable to locate the Element by Name:

Time:10-05

I am trying to automatically login to the website https://opensource-demo.orangehrmlive.com/web/index.php/auth/login using selenium By it gives an error

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

My code :

import selenium from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.chrome.service import Service

s = Service("C:\Development\chromedriver.exe")

driver = webdriver.Chrome(service=s)

driver.get("https://opensource-demo.orangehrmlive.com/")

username= driver.find_element(By.NAME,"username")

username.send_keys("Admin")

I know it sounds simple but i tried different ways to find the element..still not working.

CodePudding user response:

@Sravanthi Veera, It seems page is taking longer to load and hence find_element is fired before the page is loaded.

you can do the following:

username = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, 'username')))

#username= driver.find_element(By.NAME,"username") # commented as we are getting the element from above line. 

username.send_keys("Admin")

you will have to import the following for it to work:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
  • Related