学习目的:
掌握显示等待
掌握二次封装
正式步骤:
step1:显示等待的代码示例
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import os
dr = webdriver.Chrome()
url = 'http://renren.com/'
dr.get(url)
# dr 表示打卡浏览器
# 30 表示超时总时长 30s
# 1 表示循环查询时间,默认时间间隔0.5s
# lambda x : x.find_element_by_xpath('//*[@id="email"]') 默认的官方格式,照旧
x = WebDriverWait(dr,30,1).until(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('308@qq.com')
y.send_keys('*******')
z.click()
step2: 二次封装
# -*- 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()
来源:oschina
链接:https://my.oschina.net/u/4347380/blog/4435837