如何在python中使用selenium的示例
最近基于selenium写了一个python小工具,记录下学习记录,自己运行的环境是Ubuntu14.04.4,Python2.7,Chromium49.0,ChromeDriver2.16
selenium简介
selenium提供了一个通用的接口,可模拟用户来操作浏览器,比如用于自动化测试等.
selenium的核心是WebDriver,它提供了一组接口,这些接口能够操作各种跨平台的浏览器.各大浏览器厂商.
各大浏览器厂商也支持Selenium,将其作为浏览器的一部分.
selenium工具集提供了WebDriver,SeleniumIDE,Selenium-Grid等
Selenium1.0+WebDriver=Selenium2.0
SeleniumWebDriver是SeleniumRemoteControl(Selenium-RC)的继承者.
- WebDriver提供了更简单和简洁的接口,克服了Selenium-RCAPI一些限制.
- 相比Selenium1.0,WebDriver是面向对象式的服务.
- WebDriver驱动浏览器更有效率,提供了比Selenium1.0更多的功能
- SeleniumRC只能在单机上运行,WebDriver则提供了远程操作的功能
selenium基本使用
selenium运行需要什么
主要包括三部分:seleniumselenium,浏览器driver,浏览器seleniumselenium是一组通用的接口,而不同的浏览器提供其自身的driver(大部分是官方的),浏览器则被模拟控制操作的终端.
安装
pipinstallselenium--upgrade apt-getinstallchromium-browser wgethttp://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconfLONG_BIT`.zip unzipchromedriver_linux32.zip cpchromedriver/usr/local/share chmod+x/usr/local/share/chromedriver ln-s/usr/local/share/chromedriver/usr/local/bin/chromedriver ln-s/usr/bin/chromedriver/usr/local/share/chromedriver
简单的使用
fromseleniumimportwebdriver
driver=webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get('http://mail.sina.net');
print(driver.title)
API使用
可参考/usr/local/lib/python2.7/dist-packages/selenium
ChromeWebDriver
selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver',port=0,chrome_options=None,service_args=None,desired_capabilities=None,service_log_path=None)
ChromeOptions
可以通过ChromeDriversession配置ChromeDriversessionChromeDriverconvenientmethodsforsettingChromeDriver-specificcapabilities
fromselenium.webdriver.chrome.optionsimportOptions
chrome_options=Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-logging')
chrome_options.add_experimental_option('prefs',{'download.default_directory':
'/tmp'})
chrome_options.binary_location='/usr/bin/chromium-browser'
driver=webdriver.Chrome(chrome_options=chrome_options)
直接使用DesiredCapabilities
ChromeOptions是构建在DesiredCapabilities之上的,为了使用DesiredCapabilities,必须知道capability的Key/value对.
chrome_options=Options()
capabilities={}
capabilities['platform']="WINDOWS"
capabilities['version']="10"
capabilities.update(chrome_options.to_capabilities())
driver=webdriver.Chrome(desired_capabilities=capabilities)
chromedriver运行方式
TheChromeDriverclass不断的创建实例,会浪费很多的时间,可以通过两个方式解决.
使用ChromeDriverService
importselenium.webdriver.chrome.serviceasservice
service=service.Service('/usr/bin/chromedrive')
service.start()
capabilities={}
driver=webdriver.Remote(service.service_url,capabilities)
driver.get('http://mail.sina.net');
print(driver.title)
开启单独的ChromeDriver服务
./chromedriver
driver=webdriver.Remote('http://127.0.0.1:9515',DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');
RemoteWebDriverServer
TheRemoteWebDriveriscomposedoftwopieces:aclientandaserver.TheclientisyourWebDrivertestandtheserverissimplyaJavaservlet,whichcanbehostedinanymodernJEEappserver.Theserverwillalwaysrunonthemachinewiththebrowseryouwanttotest.
wgethttp://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
java-jarselenium-server-standalone-2.53.0.jar
fromseleniumimportwebdriver
fromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilities
driver=webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',des
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。