Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python selenium Element error event wait function is not helping

This is my first attempt to login a website using selenium, i have written the below piece of code but not able to login the website, getting the following error message.

Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="txtUsername"]"}
(Session info: chrome=108.0.5359.95)

  from selenium import webdriver
  from selenium.webdriver.chrome.service import Service
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.wait import WebDriverWait
  s=Service("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
  driver = webdriver.Chrome(service=s)
  driver.maximize_window()
  driver.get("https://opensource-demo.orangehrmlive.com")
  wait = WebDriverWait(driver, 10)
  driver.find_element(By.NAME,"txtUsername").send_keys("Admin")
  driver.find_element(By.ID, "txtPassword").send_keys("admin123")
  driver.quit()



  

Looking for some suggestions

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are 2 problems with your code:

  1. You created the wait object but not used it…
  2. Your locators are wrong.
    The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)

wait = WebDriverWait(driver, 20)

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

wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("Admin")
wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys("admin123")
wait.until(EC.element_to_be_clickable((By.TAG_NAME, "button"))).click()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading