使用expected_conditions的url_changes方法判断是否登录成功

使用expected_conditions的url_changes方法判断是否跳转页面登录成功

 from selenium import webdriver
 from selenium.webdriver.support import expected_conditions as ec
 from selenium.webdriver.support.wait import WebDriverWait

 def login():
     driver = webdriver.Chrome()
     wait = WebDriverWait(driver, 10)
     driver.get("http://xxx.com")

     # 账号
     driver.find_element(By.XPATH, "//input[@id='txtUName']").send_keys(username)
     # 密码
     driver.find_element(By.XPATH, "//input[@id='txtPassword']").send_keys(passwd)
     # 登录之前的url
     login_url=self.driver.current_url
     driver.find_element(By.XPATH, "//input[@id='btnLogin']").click()

     # 登录之后的url
     sjurl=self.wait.until(ec.url_changes(login_url))


     # 如果地址变化结果为True,说明登录成功,否则登录失败
     assert sjurl==True,"登录失败"
posted @ 2024-06-16 22:06  cherish-github123  阅读(17)  评论(0编辑  收藏  举报