1

This is my first attempt to login a website using selenium, I have written the below piece of code

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

but I'm 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)

Looking for some suggestions

RF1991
  • 2,037
  • 4
  • 8
  • 17

2 Answers2

1

The wait needs to be called:

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)
user = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))
user.send_keys("Admin")
driver.find_element(By.ID, "txtPassword").send_keys("admin123")
driver.quit()
CodeKorn
  • 300
  • 1
  • 8
  • Hi Codkron, thanks for looking into it but getting the below error : Message: element not interactable – user20432746 Dec 06 '22 at 19:32
  • Does this help https://stackoverflow.com/questions/44119081/how-do-you-fix-the-element-not-interactable-exception – CodeKorn Dec 06 '22 at 19:47
1

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()
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thanks prophet, it worked, one question (C:\webdrivers\chromedriver.exe') in my case exe file is placed in a different location but still your code works, how is that possible? – user20432746 Dec 06 '22 at 19:33
  • I don't know. This should not work. – Prophet Dec 06 '22 at 19:35
  • not sure how it worked, one more thing to know is lets suppose after login into the website, if i need to select a field from drop down and then click on the checkbox button as per the choice and then download the file, do we have such provisions in selenium ? – user20432746 Dec 06 '22 at 19:37
  • Generally you can perform with Selenium every action a human user can do on a web page via the GUI: click elements, read texts, open drop-down menus, check/uncheck checkboxes etc. If you have any specific questions please ask them in separate questions. Stackoverflow guidelines are asking to limit each question for one specific issue. – Prophet Dec 06 '22 at 19:42