如何在python中使用selenium的示例
Python  /  管理员 发布于 7年前   195
最近基于selenium写了一个python小工具,记录下学习记录,自己运行的环境是Ubuntu 14.04.4, Python 2.7,Chromium 49.0,ChromeDriver 2.16
selenium简介
selenium提供了一个通用的接口,可模拟用户来操作浏览器,比如用于自动化测试等.
selenium的核心是WebDriver,它提供了一组接口,这些接口能够操作各种跨平台的浏览器.各大浏览器厂商.
各大浏览器厂商也支持Selenium,将其作为浏览器的一部分.
selenium工具集提供了WebDriver,Selenium IDE,Selenium-Grid等
Selenium 1.0 + WebDriver = Selenium 2.0
Selenium WebDriver是Selenium Remote Control(Selenium-RC)的继承者.
selenium基本使用
selenium运行需要什么
主要包括三部分:selenium selenium,浏览器driver,浏览器selenium selenium是一组通用的接口,而不同的浏览器提供其自身的driver(大部分是官方的),浏览器则被模拟控制操作的终端.
安装
pip install selenium --upgradeapt-get install chromium-browserwget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconf LONG_BIT`.zipunzip chromedriver_linux32.zipcp chromedriver /usr/local/sharechmod +x /usr/local/share/chromedriverln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver ln -s /usr/bin/chromedriver /usr/local/share/chromedriver
简单的使用
from selenium import webdriverdriver = webdriver.Chrome('/usr/local/bin/chromedriver')driver.get('http://mail.sina.net');print(driver.title)
API使用
可参考/usr/local/lib/python2.7/dist-packages/selenium
Chrome WebDriver
复制代码 代码如下:
selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)
ChromeOptions
可以通过ChromeDriver session配置ChromeDriver session ChromeDriverconvenient methods for setting ChromeDriver-specific capabilities
from selenium.webdriver.chrome.options import Optionschrome_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运行方式
The ChromeDriver class不断的创建实例,会浪费很多的时间,可以通过两个方式解决.
使用ChromeDriverService
import selenium.webdriver.chrome.service as serviceservice = 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服务
./chromedriverdriver = webdriver.Remote('http://127.0.0.1:9515', DesiredCapabilities.CHROME)driver.get('http://mail.sina.net');
RemoteWebDriverServer
The RemoteWebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a Java servlet, which can be hosted in any modern JEE app server. The server will always run on the machine with the browser you want to test.
wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jarjava -jar selenium-server-standalone-2.53.0.jarfrom selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdriver = webdriver.Remote( command_executor='http://127.0.0.1:4444/wd/hub',desdesired_capabilities=DesiredCapabilities.CHROME)driver.get('http://mail.sina.net');
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号