Reputation: 148
When writing an auto-it for safari with selenium, is it possible to know if a password inserted into a website login is wrong?
I've tried using try:
and except:
, but selenium doesn't raise an error when the password is wrong.
an example would be:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Safari()
driver.get(r'website')
usr=driver.find_element_by_name(r'username')
usr.clear()
usr.send_keys(r'username')
pas=driver.find_element_by_name(r'password')
pas.clear()
pas.send_keys(r'password')
pas.send_keys(Keys.RETURN)
Upvotes: 0
Views: 1147
Reputation: 193208
To validate if a username/password inserted into a website login is wrong you can use a try/catch{}
block and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://auth.florimont.ch/cas/login?service=https%3A%2F%2Fflore.florimont.ch%2Fhome")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("a_random_programmer")
driver.find_element_by_css_selector("input#password").send_keys("a_random_programmer")
driver.find_element_by_css_selector("input.btn-submit").click()
try:
WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.XPATH, "//h2[text()='Enter your Username and Password']//preceding::div[1]"), "Invalid credentials."))
print("Invalid Login")
except TimeoutException as e:
print("Valid Login")
driver.quit()
Console Output:
Invalid Login
Upvotes: 0
Reputation: 33384
Use WebDriverWait
and element_to_be_clickable
and following css selector.
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
driver=webdriver.Chrome()
driver.get(r'https://auth.florimont.ch/cas/login;jsessionid=8B5A3B65779756F3CF71ACB41E578AC3?service=https%3A%2F%2Fflore.florimont.ch%2Fhome')
usr=driver.find_element_by_name(r'username')
usr.clear()
usr.send_keys(r'username')
pas=driver.find_element_by_name(r'password')
pas.clear()
pas.send_keys(r'password')
pas.send_keys(Keys.RETURN)
print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#fm1 #msg"))).text)
Print on console:
Invalid credentials.
Browser snapshot:
Upvotes: 2