设为首页 收藏本站
查看: 1057|回复: 0

[经验分享] Selenium Python bindings 文档一

[复制链接]

尚未签到

发表于 2015-4-22 07:54:50 | 显示全部楼层 |阅读模式

  • 安装
  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用户的详细指示
  注意:为完成安装,必须有网络连接


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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59359-1-1.html 上篇帖子: Python快速学习10: 循环的对象及设计 (生活的规律) 下篇帖子: Unicode和Python的中文处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表