乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > word下载安装-Python Selenium中文文档-安装篇

word下载安装-Python Selenium中文文档-安装篇

作者:乔山办公网日期:

返回目录:word文档

Selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。


1. 安装¶


1.1. 介绍¶


Selenium Python提供了基于Selenium WebDriver一个简单的API 编写功能/验收测试。 通过Selenium Python API,可以直观地访问Selenium WebDriver的所有功能。


Selenium Python提供了一个方便的API来访问Selenium WebDrivers,如Firefox,Ie,Chrome,Remote等。目前支持的Python版本是2.7,3.5及更高版本。


本文档介绍了Selenium 2 WebDriver API。 Selenium 1 / Selenium RC API不在此处。


1.2. 下载Selenium Python库¶


可以从PyPI页面下载Selenium的Python库。 但是,更好的方法是使用pip来安装selenium包。 Python 3.6在标准库中提供了pip。 使用pip,你可以像这样安装selenium:


pip install selenium


可以考虑使用virtualenv来创建隔离的Python环境。 Python 3.6有pyvenv,几乎与virtualenv相同。


1.3. 驱动¶


Selenium需要驱动程序与所选浏览器进行交互。 例如,Firefox需要geckodriver,需要在运行以下示例之前安装geckodriver。 确保它配置在你的PATH路径里。 例如,将它放在/ usr / bin或/ usr / local / bin中。


如果不遵守此步骤,将会出现错误selenium.common.exceptions.WebDriverException:消息:'geckodriver'可执行文件需要位于PATH中。


其他支持的浏览器将拥有自己的驱动程序。 下面是一些比较流行的浏览器驱动程序的链接。


Chrome:


https://sites.google.com/a/chromium.org/chromedriver/downloads


Edge:


https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/


Firefox:


https://github.com/mozilla/geckodriver/releases


Safari:


https://webkit.org/blog/6900/webdriver-support-in-safari-10/


1.4. Windows用户的详细说明¶


Note


你需要链接Internet执行以下安装


1. 安装Python 3.6 ,来自于 python.org/download下的对应MSI安装程序


2. 执行cmd.exe(命令行窗口), 按照如下命令运行pip 安装selenium.


C:Python35Scriptspip.exe install selenium


现在可以使用Python运行测试脚本。 例如,如果已创建基于Selenium的脚本并将其保存在C:my_selenium_script.py


可以用以下命令运行:


C:Python35python.exe C:my_selenium_script.py


1.5. 下载Selenium服务器¶


Note


仅当你要使用远程WebDriver时才需要Selenium服务器。 有关详细信息,请参阅使用Selenium和远程WebDriver部分。


https://selenium-python.readthedocs.io/getting-started.html#selenium-remote-webdriver


如果您是初学者学习Selenium,可以跳过本节并继续下一章。


Selenium服务器是一个Java程序。 建议使用Java Runtime Environment(JRE)1.6或更高版本来运行Selenium服务器。


可以从selenium网站的下载页面下载Selenium server 2.x. 文件名应该是这样的:selenium-server-standalone-2.x.x.jar。 始终可以下载最新的2.x版Selenium服务器。


如果系统中未安装Java Runtime Environment(JRE),则可以从Oracle网站下载JRE。 https://www.oracle.com/technetwork/java/javase/downloads/index.html


如果您使用的是GNU / Linux系统并且在系统中具有root访问权限,则还可以使用操作系统命令安装JRE。


如果PATH(环境变量)中有java命令,则可以使用以下命令启动Selenium服务器:


java -jar selenium-server-standalone-2.x.x.jar


Replace 2.x.x with the actual version of Selenium server you downloaded from the site.


将2.x.x替换为从站点下载的Selenium服务器的实际版本。


如果JRE以非root用户身份安装和/或在PATH(环境变量)中不可用,则可以键入java命令的相对路径或绝对路径。 同样,您可以提供Selenium服务器jar文件的相对路径或绝对路径。 然后,该命令将如下所示:


/path/to/java -jar /path/to/selenium-server-standalone-2.x.x.jar


引用部分:Selenium server使用方法¶


要使用远程WebDriver,您应该运行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连接到远程WebDriver。 这里有些例子:


from selenium import webdriver


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 is a dictionary 类型, 因此可以替换缺省字典,你可以设置具体的值:


driver = webdriver.Remote(


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


desired_capabilities={'browserName': 'htmlunit',


'version': '2',


'javascriptEnabled': True})


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


请关注Pyhelloworld头条号,了解更多精彩内容


相关阅读

  • word下载安装-Python Selenium中文文档-安装篇

  • 乔山办公网word文档
  • Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网