so I am trying to log into this website called ttrockstars, and I have written a function to login to the website. However the code seems to be running too fast and ignoring time.sleep(5)
that i have added to the code:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time PATH = r"C:UsersshahfDesktopchromedriver.exe" driver = webdriver.Chrome(PATH) driver.get("https://play.ttrockstars.com/auth/school/student") def login(school, username, password): driver.find_element_by_xpath("//input[1]").send_keys(school) driver.find_element_by_xpath("//input[1]").send_keys(Keys.RETURN) time.sleep(5) driver.find_element_by_xpath("//input[1]").send_keys(username) driver.find_element_by_id("mat-input-2").send_keys(password) driver.find_element_by_id("mat-input-2").send_keys(Keys.RETURN) login("my school", "my username", "my password")
When I run this, it simply enters the school name and then deletes it and enters the username and then brings up an error saying the password box does not exist. This would not be a problem if it actually submitted the school and waited 5 seconds. Why is this happening and is there a workaround?
Advertisement
Answer
These are the steps to follow to achieve what you want:
- Sleep 2-3 seconds after filling school name.
- Get the drop-down menu element by its class and get
option
element by the drop-down menu element and click it. - Sleep 2-3 seconds so Username-Password page gets loaded.
- Fill username and password and click enter
Note: This will click the first choice from the school choices.
Here is the code:
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time PATH = r"C:UsersshahfDesktopchromedriver.exe" driver = webdriver.Chrome(PATH) driver.get("https://play.ttrockstars.com/auth/school/student") def login(school, username, password): driver.find_element_by_xpath("//input[1]").send_keys(school) time.sleep(3) driver.find_element_by_class_name("mat-autocomplete-panel").find_element_by_class_name("mat-option").click() time.sleep(3) driver.find_element_by_xpath("//input[1]").send_keys(Keys.RETURN) time.sleep(5) driver.find_element_by_xpath("//input[1]").send_keys(username) driver.find_element_by_id("mat-input-2").send_keys(password) driver.find_element_by_id("mat-input-2").send_keys(Keys.RETURN) login("my school", "my username", "my password")