I am trying to optimize the code below to log in to the system, using try ... except. My first problem is that try except takes too long, because always looking for a certain element on the page.
My questions are:
1) This can be done other than using the element?
2) There are different options to set the wait than the timeout (f.e .: wrongcred = WebDriverWait (self.driver, timeout = 1))?
Thanks for response.
def test_role(self, u=None, p=None):
if u is not None:
print("Start test...")
username = self.driver.find_element_by_id("username")
username.clear()
username.send_keys(u)
password = self.driver.find_element_by_id("password")
password.clear()
self.driver.implicitly_wait(30)
password.send_keys(p)
self.driver.implicitly_wait(30)
self.driver.find_element_by_id("kc-login").click()
try:
WebDriverWait(self.driver, timeout=1).until(EC.visibility_of_element_located((By.XPATH, "//h2[contains(text(),'workplace s')]")))
print("Found")
logout = self.driver.find_element_by_xpath("//mat-icon[@class='mat-icon notranslate material-icons mat-icon-no-color']")
print("Odhlaseni z AISG")
logout.click()
except:
print("Not found")
try:
wrongcred = WebDriverWait(self.driver, timeout=1).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='kc-feedback-text']")))
assert wrongcred.text == "Invalid username or password."
print("wrongcred")
except:
print("change password to all123546")
username = self.driver.find_element_by_id("password-new")
username.clear()
username.send_keys("all123546")
password = self.driver.find_element_by_id("password-confirm")
password.clear()
password.send_keys("all123546")
self.driver.find_element_by_id("kc-form-buttons").click()
logout = self.driver.find_element_by_xpath(
"//mat-icon[@class='mat-icon notranslate material-icons mat-icon-no-color']")
print("Logout")
logout.click()
print("Continue with next username")
else:
print("Tested with all users.")