from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from time import sleep driver=webdriver.Chrome() #隐式等待 driver.implicitly_wait(10) #最大化窗体 driver.maximize_window() driver.get('http://39.98.138.157/shopxo/index.php') driver.find_element_by_xpath('//a[text()="登录"]').click() driver.find_element_by_xpath( '//input[@name="accounts"]').send_keys('666666') driver.find_element_by_xpath( '//input[@name="pwd"]').send_keys('111111') driver.find_element_by_xpath( '/html/body/div[4]/div/div[2]/div[2]/form/div[3]/button').click() # ①用于校验元素是否登录成功,try: except: try: # 显示等待,until成功则不输出message的信息,until失败则输出message WebDriverWait(driver,10,0.5).until( lambda el:driver.find_element_by_xpath( '//a[text()="退出"]'),message='登录失败' ) except Exception as e: #抛出异常 print('登录失败,信息不正确:'.format(e)) # #②断言的校验实际本质就是if else # el=driver.find_element_by_xpath('//a[text()="退出"]') # txt=el.text #txt=退出 # # if txt=='退出': # print('操作成功') # else: # print('fales') sleep(5) driver.quit()