用于selenium的python绑定

selenium的Python项目详细描述


简介

selenium webdriver的python语言绑定。

selenium包用于从python自动进行web浏览器交互。

Home:http://www.seleniumhq.org
Docs:selenium package API
Dev:https://github.com/SeleniumHQ/Selenium
PyPI:https://pypi.org/project/selenium/
IRC:#selenium channel on freenode

支持多种浏览器/驱动程序(Firefox、Chrome、Internet Explorer)以及远程协议。

支持的Python版本

  • Python2.7,3.4+

安装

如果您的系统上有pip,您可以简单地安装或升级python绑定:

pip install -U selenium

或者,您可以从PyPI(例如selenium-3.141.0.tar.gz)下载源发行版,将其取消归档,然后运行:

python setup.py install

注意:您可能需要考虑使用virtualenv来创建独立的python环境。

驱动程序

selenium需要一个驱动程序来与所选浏览器交互。火狐, 例如,需要geckodriver,在运行下面的示例之前需要安装该程序。确保它在您的路径中,例如,将它放在/usr/bin/usr/local/bin中。

如果不遵守此步骤,将出现一个错误selenium.common.exceptions.webdriverexception:消息:“geckodriver”可执行文件需要在路径中。

其他受支持的浏览器将提供自己的驱动程序。下面是一些更流行的浏览器驱动程序的链接。

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/

示例0:

  • 打开新的Firefox浏览器
  • 在给定的url加载页面
fromseleniumimportwebdriverbrowser=webdriver.Firefox()browser.get('http://seleniumhq.org/')

示例1:

  • 打开新的Firefox浏览器
  • 加载雅虎主页
  • 搜索“seleniumhq”
  • 关闭浏览器
fromseleniumimportwebdriverfromselenium.webdriver.common.keysimportKeysbrowser=webdriver.Firefox()browser.get('http://www.yahoo.com')assert'Yahoo'inbrowser.titleelem=browser.find_element_by_name('p')# Find the search boxelem.send_keys('seleniumhq'+Keys.RETURN)browser.quit()

例2:

selenium webdriver通常用作测试web应用程序的基础。下面是一个使用python标准unittest库的简单示例:

importunittestfromseleniumimportwebdriverclassGoogleTestCase(unittest.TestCase):defsetUp(self):self.browser=webdriver.Firefox()self.addCleanup(self.browser.quit)deftestPageTitle(self):self.browser.get('http://www.google.com')self.assertIn('Google',self.browser.title)if__name__=='__main__':unittest.main(verbosity=2)

硒服务器(可选)

对于普通的webdriver脚本(非远程),不需要java服务器。

但是,要使用SeleniumWebDriverRemote或传统的SeleniumAPI(SeleniumRC),还需要运行Selenium服务器。服务器需要Java运行时环境(JRE)。

分别从以下位置下载服务器:http://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.0.jar

从命令行运行服务器:

java -jar selenium-server-standalone-3.141.0.jar

然后运行python客户端脚本。

使用来源卢克!

联机查看源代码:

official:https://github.com/SeleniumHQ/selenium/tree/master/py

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使Eclipse在其控制台中显示最顶层的异常,而不是完整的堆栈跟踪   java如何为一个组件提供多个DropTargetListener?   在Eclipse包资源管理器中,有些文件不可见?   java在Spring Boot中使用@Bean配置设置类属性的默认值   在JTextPane中使用#链接的Java HTML?   java当应用程序打开时,如何将通知内容发送给活动?   java Android ROOM如何编写包含多个实体的查询,这是在哪里完成的?   Play Framework的java登录/注销问题?   java如何从安卓 Cordova/Phonegap调用javascript函数   JavaFX8如何在Java8中显示上次修改的LocalDateTime?   javabean验证中的多个约束注释   java使用JSTL设置请求属性   java在Android启动的服务中调用函数   用于检查xml是否包含键和值的java XPath表达式   在java游戏中使用斜坡因子挥杆   Java文件:尝试使用FileWriter将结果附加到已经存在的文件中   bootclasspath Java Xbootclasspath,相对路径   java我如何让这个“怪物战斗模拟器”工作?   swing使用动作侦听器隐藏和显示java桌面应用程序