Learning objectives:
Master display wait
Master the second package
Official steps:
step1: display example of the code waits
# - * - Coding: UTF-. 8 - * - from Selenium Import the webdriver from selenium.webdriver.support.ui Import WebDriverWait Import Time Import OS DR = webdriver.Chrome () URL = ' http://renren.com/ ' DR .get (URL) # DR represents punch browser # 30 represents the total duration of the timeout 30s # . 1 represents the cycle time of the query, the default time interval of 0.5s # the lambda X: x.find_element_by_xpath ( '// * [@ ID = "In Email"] ') official default format, as usual X = WebDriverWait (DR, 30,1) .until ( the lambda X: x.find_element_by_xpath ( '//*[@id="email"]')) y = WebDriverWait(dr,30,1).until(lambda x : x.find_element_by_xpath('//*[@id="password"]')) z = WebDriverWait(dr,30,1).until(lambda x : x.find_element_by_xpath('//*[@id="login"]')) x.send_keys('[email protected]') y.send_keys('*******') z.click()
step2: Secondary Packaging
# -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By import time import os class xianshidengdai(): def __init__(self,driver): self.driver = driver self.timeout = 30 self.t = 0.5 def findelement(self,locator): element = WebDriverWait(self.driver,self.timeout,self.t).until(lambda x : x.find_element(*locator)) return element if __name__ == "__main__": driver = webdriver.Chrome() driver.get('http://renren.com') test = xianshidengdai(driver) loc1 = (By.XPATH ,'//*[@id="email"]') test.findelement(loc1).send_keys('***') driver.close()