SDOFPKSODPFKSDPF
simpleelistener的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”可执行文件需要在路径中。
其他受支持的浏览器将提供自己的驱动程序。下面是一些更流行的浏览器驱动程序的链接。
示例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客户端脚本。