安装:
pip install -U selenium
文档
需要选择一个合适IDE,要求如下:
-
代码完成和智能提示的图形化代码编辑器
-
函数和类的代码浏览器
-
语法高亮
-
项目管理
-
代码模板
-
单元测试和调试
-
源代码控制支持
推荐:WingIDE,PyCharm,PyDev Eclipse plugin,PyScripter。相关下载地址如下:
实例1:在网站python自动化测试上面寻找webdriver相关的页面,并输出相关的网址:
注意:本实例因为http://automationtesting.vipsinaapp.com及http://automationtesting.sinaapp.com已经因为新浪收费原因关闭匿名用户查询,已经不能执行。
#!/usr/bin/env python
# encoding: utf-8
import time
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()
time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.get_attribute('href')
# close the browser window
driver.quit()
selenium.webdriver实现了浏览器驱动类,涉及 Firefox, Chrome, Internet Explorer, Safari等浏览器及用于测试远程机器的类RemoteWebDriver。
driver.implicitly_wait(30)表示最多等待页面打开的时间为30秒。
执行结果:
Found 2 pages:
http://automationtesting.sinaapp.com/blog/python_selenium1
http://automationtesting.sinaapp.com/blog/appium
实例2打开网址,搜索产品,并列出产品名。实例的网站为:magentocommerce,代码下载地址:learnsewithpython。
注意:上面演示的网址可能需要翻墙
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.text
# close the browser window
driver.quit()
执行结果:
Found 2 products:
MADISON EARBUDS
MADISON OVEREAR HEADPHONES
跨浏览器支持
IE
selenium下载页面地址。为了在Internet Explorer上面执行,需要下载配置InternetExplorerDriver服务,它是测试脚本和Internet Explorer之间的胶水。
在上述下载页面搜索InternetExplorerDriver,下载32位或者64位版本,并解压到合适的目录。在IE7以上的版本,选择Tools 菜单下面的Internet Options,在弹出窗口选择Security标签,把每个zone的Protected Mode设置为关或者开启的情况都设置为中。另外要特别注意IE的缩放要选择为100%才正确地进行坐标对应。
实例1:
#!/usr/bin/env python
# encoding: utf-8
import time
from selenium import webdriver
ie_driver_path = "e:\IEDriverServer.exe"
# create a new Firefox session
driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()
time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.get_attribute('href')
# close the browser window
driver.quit()
实例2:
import os
from selenium import webdriver
# get the path of IEDriverServer
# dir = os.getcwd()
ie_driver_path = "e:\IEDriverServer.exe"
# create a new Internet Explorer session
driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.text
# close the browser window
driver.quit()
执行结果和火狐的类似。
参考资料:
Chrome
ChromeDriver由Chromium开发,安装方法和IE的类似,但是不需要设置Protected Mode。 实例1:
#!/usr/bin/env python
# encoding: utf-8
import time
from selenium import webdriver
# get the path of chromedriver
chrome_driver_path = r"e:\chromedriver.exe"
#remove the .exe extension on linux or mac platform
# create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://automationtesting.sinaapp.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("webdriver")
search_field.submit()
time.sleep(6)
products = driver.find_elements_by_xpath("//a[@class='searchable']")
# get the number of anchor elements found
print "Found " + str(len(products)) + " pages:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.get_attribute('href')
# close the browser window
driver.quit()
实例2:
import os
from selenium import webdriver
# get the path of chromedriver
chrome_driver_path = r"e:\chromedriver.exe"
#remove the .exe extension on linux or mac platform
# create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class='product-name']/a")
# get the number of anchor elements found
print "Found " + str(len(products)) + " products:"
# iterate through each anchor element and
# print the text that is name of the product
for product in products:
print product.text
# close the browser window
driver.quit()
参考资料: