from selenium import webdriver from time import sleep #显示等待的模块调用 from selenium.webdriver.support.ui import WebDriverWait driver=webdriver.Chrome() ''' #隐式等待 driver.implicitly_wait(5) ''' #窗体最大化 driver.maximize_window() driver.get('http://39.98.138.157/shopxo/index.php') #设置显示等待,等待元素出现,然后再进行后续操作,webdriverwait(传递的diriver参数,超时时间,检测间隔) WebDriverWait(driver=driver,timeout=5,poll_frequency=0.5).until( lambda el:driver.find_element_by_xpath("//a[text()='登录']"),message='元素定位失败' ) ''' #显示等待会返回一个webelement元素 el=WebDriverWait(driver=driver,timeout=5,poll_frequency=0.5).until( lambda el:driver.find_element_by_xpath("//a[text()='登录']"),message='元素定位失败') el.click() #此时的el.click()相当于driver.find_element_by_xpath("//a[text()='登录']").click() ''' 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() #退出 WebDriverWait(driver=driver,timeout=5,poll_frequency=0.5).until( lambda el1:driver.find_element_by_xpath("//a[text()='退出']"),message='未登录成功' ) #强制等待 sleep(5) driver.quit()