本文共 5204 字,大约阅读时间需要 17 分钟。
执行 selenium 测试过程:
Selenium WebDriver 如何与浏览器通信:
WebDriver的协议:import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Byclass TestCase(object): def __init__(self): # from .chrome.webdriver import WebDriver as Chrome # noqa self.driver = webdriver.Chrome() self.driver.get("http://www.baidu.com") self.driver.maximize_window() time.sleep(1) def execute_click_su(self): self.driver.find_element_by_id('su').click() time.sleep(3) def quit_driver(self): self.driver.quit() 最常用的是使用 id 来定位:
# id 定位元素 def test_id(self): element = self.driver.find_element_by_id('kw') element.send_keys('selenium') print(type(element)) self.execute_click_su() 元素名定位:
# name 定位 def test_name(self): # find_element_by_name 可能找到多个name元素,返回第一个 element = self.driver.find_element_by_name('wd') element.send_keys('selenium') self.execute_click_su() 链接文本定位:
# 链接文本定位 def test_linktext(self): self.test_id() self.driver.find_element_by_link_text("百度首页").click() time.sleep(3) # 部分链接定位 def test_partial_linktext(self): self.test_id() self.driver.find_element_by_partial_link_text("首页").click() time.sleep(3) xpath 定位
# xpath 定位 def test_xpath(self): # self.driver.find_element_by_xpath("//*[@id='kw']").send_keys("github") self.driver.find_element_by_xpath("//input[@id='kw']").send_keys("github") self.execute_click_su() time.sleep(3) tag 标签定位
# tag 标签名称定位 def test_tag(self): element = self.driver.find_elements_by_tag_name('input') for i in range(17): try: element[i].send_keys('git') self.execute_click_su() time.sleep(3) except Exception as e: print(e) print(element) # time.sleep(10) css selector 定位
# css selector 定位 def test_css_selector(self): self.driver.find_element_by_css_selector('#kw').send_keys('github') self.execute_click_su() time.sleep(3) 类名定位
# 类名定位 def test_class_name(self): self.driver.find_element_by_class_name('s_ipt').send_keys('github') self.execute_click_su() time.sleep(3) 通用的指定定位方式来定位
# 指定定位方式,可选 def test_all(self): self.driver.find_element(By.ID, value='kw').send_keys('git') self.execute_click_su() time.sleep(3) 运行:
if __name__ == '__main__': case = TestCase() # 8种元素定位方法: case.test_id() case.test_name() case.test_linktext() case.test_partial_linktext() case.test_xpath() case.test_tag() case.test_css_selector() case.test_class_name() case.test_all() case.quit_driver()
封装写法:
import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Bydef get_element(driver, *loc): element = driver.find_element(*loc) return elementif __name__ == '__main__': driver = webdriver.Chrome() driver.get("http://www.baidu.com") loc1 = (By.ID, 'kw') # loc2 = (By.ID, 'su') get_element(driver, *loc1).send_keys("pornhub") get_element(driver, By.ID, 'su').click() time.sleep(3) driver.quit() Selenium WebDriver 属性
| # | 属性 | 属性描述 |
|---|---|---|
| 1 | driver.name | 浏览器名称 |
| 2 | driver.current_url | 当前url |
| 3 | driver.title | 当前页面标题 |
| 4 | driver.page_source | 当前页面源码 |
| 5 | driver.current_window_handle | 窗口句柄 |
| 6 | driver.window_handles | 当前窗口所有句柄 |
使用 WebDriver 的 find 方法定位到元素后,会返回一个 WebElement 对象,该对象用来描述 web 页面上的一个元素。
WebElement 常用属性:
| # | 属性 | 属性描述 |
|---|---|---|
| 1 | id | 标示 |
| 2 | size | 宽高 |
| 3 | rect | 宽高和坐标 |
| 4 | tag_name | 标签名称 |
| 5 | text | 文本内容 |
示例:
import timefrom selenium import webdriverfrom selenium.webdriver.remote.webelement import WebElementclass TestCase(object): def __init__(self): self.driver = webdriver.Chrome() self.driver.get("http://sahitest.com/demo/linkTest.htm") def test_webelement_prop(self): e = self.driver.find_element_by_id('t1') # e1 = WebElement print(type(e))if __name__ == '__main__': case = TestCase() case.test_webelement_prop() 通过 debug 查看:
| # | 方法 | 方法描述 |
|---|---|---|
| 1 | send_keys() | 输入内容 |
| 2 | clear() | 清空内容 |
| 3 | click() | 单击 |
| 4 | get_attribute() | 获得属性值 |
| 5 | is_selected() | 是否被选中 |
| 6 | is_enabled() | 是否可用 |
| 7 | is_displayed() | 是否显示 |
| 8 | value_of_css_property() | css属性值 |
示例:
def test_webElement_method(self): e = self.driver.find_element_by_id('t1') e.send_keys('test selenium') time.sleep(2) print(e.get_attribute('type')) print(e.get_attribute('name')) print(e.get_attribute('value')) print(e.value_of_css_property('font')) print(e.value_of_css_property('color')) time.sleep(2) e.clear()def test_webElement_method2(self): # id 为 t1 的元素是在 form 表单里,所以也可以先找到 form 表单,再去找 t1 form_ele = self.driver.find_element_by_xpath("//input[@id='t1']") form_ele.find_element_by_id('t1').send_keys('666') 方法截图:
转载地址:http://htdh.baihongyu.com/