Selenium Python bindings 文档一

  1. 安装

1.1    介绍

Selenium Python bindings提供了一个简单的API使用Selenium WebDriver来写功能、验收测试。通过Selenium Python API你可以以比较直白的方式使用Selenium的所有功能。

Selenium Python bindings提供了一个方便的API来访问Selenium Webdrivers比如Firefox,IE和Chrome, 目前支持的Python版本是Python2.6和Python2.7.Python3现在还不支持。Selenium服务器是一个Java程序。 推荐使用JRE1.6或更新的版本来跑Selenium 服务器。本文主要解释了如何使用Selenium2和WebDriver API。

1.2    为Selenium下载Python bindings
你可以在PyPi page for selenium package 下载Selenium的Python binding。它有个依赖库rdflib,版本3.1.x。你也可以使用easy_install或者pip来安装bindings:easy_install selenium 或者 pip install selenium。或者你可以考虑使用virtualenv来创建独立的Python环境。

1.3    Windows用户的详细指示

注意:为完成安装,必须有网络连接

  1. 安装Python2.7,在这里使用MSI版本安装文件
  2. 安装virtualenv,下载这个Python脚本
  3. 创建虚拟环境(你必须在virtual.py所在的目录): C:\Python27\python.exe virtualenv,py selenv,这个步骤将创建一个selenv目录用来安装selenium
  4. 安装selenium:selenv\Scripts\pip.exe install selenium
  5. 现在你在这个虚拟环境里可以运行你的Python脚本: selenv\Scripts\python.exe my_selenium_script.py

1.4    下载Selenium 服务器

注意:只有在要使用远程WebDriver的时候才需要Selenium服务器。

可以在这里下载Selenium server 2.x。文件名应该像这样: selenium-server-standalone-2.x.x.jar。你可以一直下载最新的2.x版本。

如果你的电脑没有安装JRE,可以到这里下载一个。

1.5    运行Selenium server

必须要在操作系统里安装JRE。如果java命令在PATH(环境变量)里可用。可以使用下面的命令来启动Selenium server。用实际你下载的Selenium server版本号替代2.x.x。java -jar selenium-server-standalone-2.x.x.jar
 
2.      开始
2.1 简单使用说明

如果你已经安装了Selenium server和Python bindings,而且可以运行server,可以通过下面的脚本来开始使用。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
 
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()

上面的脚本可以保存成一个文件(比如:python_org_search.py),然后可以这样运行:

Python python_org_search.py

你使用的python必须安装selenium模块。

2.2 过一下这个例子

Selenium.webdriver模块提供了所有的WebDriver实现。目前支持Webdriver实现的浏览器有Firefox,Chrome,IE和远程。Keys类提供了键盘上的键,如回车,F1,ALT等。

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

接下来, 创建Firefox Webdriver的实例

driver = webdriver.Firefox()

driver.get方法将跳转到URL指定的页面。 Webdriver在返回你测试或脚本的控制之前,将会等待直到页面完全载入(也就是说,激发了“onload“事件)。如果你页面在load的时候使用了很多AJAX就不值得这么做,因为Webdriver也许不知道什么时候会完全载入。

driver.get("http://www.python.org")

下一行是一个断言来确定标题里是否含有“Python“字样。

assert "Python" in driver.title

Webd提供了很多方法使用任一个find_element_by_*方法来查找元素。比如,输入框元素可以通过它的名字属性使用find_element_by_name方法来定位。

elem = driver.find_element_by_name("q")

下面,我们发送键盘指令,这和使用键盘敲击键很类似。特殊的键可以使用selenium.webdriver.common.keys的Keys类来发送指令。

elem.send_keys("selenium")

elem.send_keys(Keys.RETURN)

在提交页面之后,你应该到了Google页面。

assert "Google" in driver.title

最后,浏览器窗口关闭。也可以调用quit方法来代替close方法。Quit方法将退出整个浏览器,而close只是关掉一个tab。但是如果只有一个tab。默认大部分的浏览器将完全退出。

Driver.close()

2.3 使用Selenium写测试

Selenium主要被用来写测试案例。你可以使用Python的unittest模块来写测试案例。下面是一个使用unittets模块的修改版本。主要用来测试Python的搜索功能。

import unittest

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

 

class PythonOrgSearch(unittest.TestCase):

 

    def setUp(self):

        self.driver = webdriver.Firefox()

 

    def test_search_in_python_org(self):

        driver = self.driver

        driver.get("http://www.python.org")

        self.assertIn("Python", driver.title)

        elem = driver.find_element_by_name("q")

        elem.send_keys("selenium")

        elem.send_keys(Keys.RETURN)

        self.assertIn("Google", driver.title)

 

    def tearDown(self):

        self.driver.close()

 

 

if __name__ == "__main__":

unittest.main()

结果如下:

Finding files... done.

Importing test modules ... done.

 

----------------------------------------------------------------------

Ran 1 test in 23.579s

 

OK

2.4 过一下这个例子

最初,先引入需要的所有基本模块。Unittest是一个基于Java的Uunit的Python内置模块,这个模块提供了组织测试案例的框架.selenium.webdirver模块提供了所有的WebDriver实现。

import unittest

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

测试案例类继承自unittest.TestCase。继承自TestCase类是告诉unittest模块的方式,即,这是个测试案例:

class PythonOrgSearch(unittest.TestCase):

setup方法是初始化的一部分,这个方法将在你写的每个测试功能之前被调用,这里是创建了Firefox WebDriver的实例

    def setUp(self):

        self.driver = webdriver.Firefox()

这是个测试案例方法。方法的第一行创建了一个到在setUp方法里创建的driver丢下的本地引用。

    def test_search_in_python_org(self):

        driver = self.driver

driver.get方法将跳转到URL指定的页面。

driver.get("http://www.python.org")

下一行是个断言来确定标题里面有Python字样。

self.assertIn("Python", driver.title)

注意:assertIn API仅在Python2.7 unittest模块里可用。

Teardown方法将在每个测试方法之后被调用。这里主要用来做所有的cleanup工作。在当前这个方法里,浏览器窗口被关掉。

    def tearDown(self):

        self.driver.close()

最后的几行是用来运行这个测试组件。

if __name__ == "__main__":

    unittest.main()

2.5 和远程WebDriver一起使用Selenium

为使用远程WebD,必须先运行Selenium服务器,可以使用下面的命令启动Selenium服务。

Java –jar selenium-server-standalone-2.x.x.jar

当运行Selenium服务的时候,你可以看到类似下面的消息:

15:43:07.541 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
上面几行是说,你可以使用这个URL连到远程WebD。下面是具体例子:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

 

driver = webdriver.Remote(

   command_executor='http://127.0.0.1:4444/wd/hub',

   desired_capabilities=DesiredCapabilities.CHROME)

 

driver = webdriver.Remote(

   command_executor='http://127.0.0.1:4444/wd/hub',

   desired_capabilities=DesiredCapabilities.OPERA)

 

driver = webdriver.Remote(

   command_executor='http://127.0.0.1:4444/wd/hub',

   desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS)

desired_capabilities是个字典,可以使用自己的设置来取代默认设置:

driver = webdriver.Remote(

   command_executor='http://127.0.0.1:4444/wd/hub',

   desired_capabilities={'browserName': 'htmlunit',

                         'version': '2',

                        'javascriptEnabled': True})

作者:Shane
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/bluescorpio/p/2498125.html