010 selenium 二次封装
Posted huafan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了010 selenium 二次封装相关的知识,希望对你有一定的参考价值。
‘‘‘ 时间:2018/12/11 功能:二次封装 目录: 一: 原始数据 二: 封装参数 三: 封装成类 四: 封装操作 五: 元素定位 ‘‘‘
一: 原始数据
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait driver = webdriver.Firefox() driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") ele1 = WebDriverWait(driver, 5, 1).until(lambda x: x.find_element_by_id("account")) print(ele1) # 返回的是element元素对象 ele1.send_keys("admin") ele2 = WebDriverWait(driver, 5, 1).until(lambda x: x.find_element_by_name("password")) ele2.send_keys("123456") ele3 = WebDriverWait(driver, 5, 1).until(lambda x: x.find_element_by_id("submit")) ele3.click()
<selenium.webdriver.remote.webelement.WebElement (session="49afdff4-bd36-496e-88b3-5be57212dff1", element="{272cf41f-3c40-4c0c-aaa9-24f95f39bd5b}")>
二: 封装参数
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By def findElement(driver, loctor, timeout = 10, time = 0.5): ele = WebDriverWait(driver, 5, 1).until(lambda x:x.find_element(*loctor)) return ele driver = webdriver.Firefox() driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") loc1 = (By.ID, "account") loc2 = (By.NAME, "password") loc3 = (By.ID, "submit") findElement(driver, loc1).send_keys("admin") findElement(driver, loc2).send_keys("123456") findElement(driver, loc3).click()
三: 封装成类
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By class Base(): def __init__(self, driver): self.driver = driver self.timeout = 10 self.time = 0.5 def find_element(self, locator): ele = WebDriverWait(self.driver, self.timeout, self.time).until(lambda x:x.find_element(*locator)) return ele if __name__ == "__main__": driver = webdriver.Firefox() driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") chandao = Base(driver) loc1 = (By.ID, "account") loc2 = (By.NAME, "password") loc3 = (By.ID, "submit") chandao.find_element(loc1).send_keys("admin") chandao.find_element(loc2).send_keys("123456") chandao.find_element(loc3).click()
四: 封装操作
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By class Base(): def __init__(self, driver): self.driver = driver self.timeout = 10 self.time = 0.5 def find_element(self, locator): ele = WebDriverWait(self.driver, self.timeout, self.time).until(lambda x:x.find_element(*locator)) return ele def send_keys(self, locator, text): ele = self.find_element(locator) ele.send_keys(text) def click(self, locator): ele = self.find_element(locator) ele.click() if __name__ == "__main__": driver = webdriver.Firefox() driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") chandao = Base(driver) loc1 = (By.ID, "account") loc2 = (By.CSS_SELECTOR, "[name = ‘password‘]") # CSS语法 loc3 = (By.XPATH, ".//*[@id=‘submit‘]") # xpath语法 chandao.send_keys(loc1, "admin") chandao.send_keys(loc2, "123456") chandao.click(loc3)
五: 元素定位
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait class Base(): def __init__(self, driver): self.driver = driver self.timeout = 10 self.time = 0.5 def find_element(self, locator): ele = WebDriverWait(self.driver, self.timeout, self.time).until(lambda x:x.find_element(*locator)) return ele def send_keys(self, locator, text): ele = self.find_element(locator) ele.send_keys(text) def click(self, locator): ele = self.find_element(locator) ele.click() if __name__ == "__main__": driver = webdriver.Firefox() driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html") chandao = Base(driver) loc1 = ("id", "account") loc2 = ("css selector", "[name = ‘password‘]") # CSS语法 loc3 = ("xpath", ".//*[@id=‘submit‘]") # xpath语法 chandao.send_keys(loc1, "admin") chandao.send_keys(loc2, "123456") chandao.click(loc3)
以上是关于010 selenium 二次封装的主要内容,如果未能解决你的问题,请参考以下文章
Selenium2+python自动化63-二次封装(click/send_kesy)