Selenium使用
《Flask Web开发》一书中提到使用Selenium
进行端到端的测试,Selenium
是一组多平台的浏览器自动化工具。我们这里主要使用WebDriver
来模拟打开Chrome
访问网页的功能。
安装
我们使用pip
工具来安装selenium
(Mac OS X)。
pip install selenium
同时安装Chrome
驱动
brew install chromedriver
,记下安装路径。
简单使用
我使用Selenium
来访问http://qinf.github.io/
,代码如下:
#!/usr/bin/env python
# encoding: utf-8
from selenium import webdriver
import os
chromedriver = "/usr/local/Cellar/chromedriver/2.18/bin/chromedriver" # chromedirver可执行文件路径
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
# driver = webdriver.Safari()
driver.get('http://qinf.github.io')
print driver.title
driver.quit()
当然如果把selenium
放到系统路径里面会简略好多,代码如下:
from selenium import webdriver
dirver = webdriver.Chrome()
driver.get('http://qinf.github.io')
print driver.title # 打印标题
driver.quit() # 退出
Selenium-WebDriver API 命令与操作
抓出网页
通过调用get
来打开一个页面:
dirver.get('http://qinf.github.io')
定位UI元素
我可以通过WebDriver
实例或者WebElement
来定位一个UI
元素。
通过id
找到下面的元素
<div id="myId">...</div>
element = driver.find_element_by_id("myId")
or
form selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="myId")
通过Class Name
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
cheeses = driver.find_elements_by_class_name("cheese")
# or
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")
通过Tag Name
<iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")
# or
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")
通过Name
<input name="cheese" type="text"/>
cheese = driver.find_element_by_name("cheese")
# or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")
通过Link Text
<a href="http://www.google.com/search?q=cheese">cheese</a>>
cheese = driver.find_element_by_link_text("cheese")
# or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.LINK_TEXT, "cheese")
通过Partial Link Text
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
cheese = driver.find_element_by_partial_link_text("cheese")
# or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")
通过CSS
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")
# or
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
通过XPATH
<input type="text" name="example" />
<INPUT type="text" name="other" />
inputs = driver.find_elements_by_xpath("//input")
# or
from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")
通过JavaScript
# 获取页面上加载jQuery的
element = driver.execute_script("return $('.cheese')[0]")
# 获取页面上每个标签的输入元素
labels = driver.find_elements_by_tag_name("label")
inputs = driver.execute_script(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels)
用户输入
处理select
复选框
# 找到页面中的第一个select,然后循环选中每个option
select = driver.find_element_by_tag_name("select")
allOptions = select.find_elements_by_tag_name("option")
for option in allOptions:
print "Value is: " + option.get_attribute("value")
option.click()
# 更高效的做法使用"Select"函数
# available since 2.12
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.deselect_all() # 取消所有选择
select.select_by_visible_text("Edam") # 选择展示文本为Edam的复选框
submit
操作
driver.find_element_by_id("submit").click()
or element.submit()
在Windows和Frames之间移动
# 在命名windows间跳转
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
driver.switch_to.window("windowName")
# 迭代处理每个window
for handle in driver.window_handles:
driver.switch_to.window(handle)
# frame之间跳转
driver.switch_to.frame("frameName")
弹出框
alert = dirver.switch_to.alert
历史
driver.forward() # 前进
driver.back() # 后退
Cookies
# 添加cookie
driver.add_cookie({'name':'key', 'value':'value'})
# 打印cookie
for cookie in driver.get_cookies():
print '%s --> %s' % (cookie['name'], cookie['value'])
# 删除cookie
driver.delete_cookie('value')
driver.delete_all_cookies()