如何在python中使用selenium的示例


Posted in Python onDecember 26, 2017

最近基于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)的继承者.

  1. WebDriver提供了更简单和简洁的接口,克服了Selenium-RC API一些限制.
  2. 相比Selenium 1.0,WebDriver是面向对象式的服务.
  3. WebDriver驱动浏览器更有效率,提供了比Selenium 1.0更多的功能
  4. Selenium RC只能在单机上运行,WebDriver则提供了远程操作的功能

selenium基本使用

selenium运行需要什么

主要包括三部分:selenium selenium,浏览器driver,浏览器selenium selenium是一组通用的接口,而不同的浏览器提供其自身的driver(大部分是官方的),浏览器则被模拟控制操作的终端.

安装

pip install selenium --upgrade
apt-get install chromium-browser
wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux`getconf LONG_BIT`.zip
unzip chromedriver_linux32.zip
cp chromedriver /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

简单的使用

from selenium import webdriver
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

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 Options
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运行方式

The ChromeDriver class不断的创建实例,会浪费很多的时间,可以通过两个方式解决.

使用ChromeDriverService

import selenium.webdriver.chrome.service as service
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

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.jar
java -jar selenium-server-standalone-2.53.0.jar

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',des
desired_capabilities=DesiredCapabilities.CHROME)
driver.get('http://mail.sina.net');

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Web框架Pylons中使用MongoDB的例子
Dec 03 Python
总结网络IO模型与select模型的Python实例讲解
Jun 27 Python
python递归查询菜单并转换成json实例
Mar 27 Python
Python AES加密实例解析
Jan 18 Python
pandas 使用apply同时处理两列数据的方法
Apr 20 Python
python使用Flask操作mysql实现登录功能
May 14 Python
python pandas 对series和dataframe的重置索引reindex方法
Jun 07 Python
python生成n个元素的全组合方法
Nov 13 Python
Python匿名函数/排序函数/过滤函数/映射函数/递归/二分法
Jun 05 Python
使用K.function()调试keras操作
Jun 17 Python
python中rb含义理解
Jun 18 Python
python在地图上画比例的实例详解
Nov 13 Python
Python使用Matplotlib实现Logos设计代码
Dec 25 #Python
利用Python2下载单张图片与爬取网页图片实例代码
Dec 25 #Python
Python实现生成随机数据插入mysql数据库的方法
Dec 25 #Python
python数据抓取分析的示例代码(python + mongodb)
Dec 25 #Python
Python实现生成随机日期字符串的方法示例
Dec 25 #Python
浅谈Python NLP入门教程
Dec 25 #Python
Python图形绘制操作之正弦曲线实现方法分析
Dec 25 #Python
You might like
php日历[测试通过]
2008/03/27 PHP
Windows下利用Gvim写PHP产生中文乱码问题解决方法
2011/04/20 PHP
最新版本PHP 7 vs HHVM 多角度比较
2016/02/14 PHP
thinkPHP框架通过Redis实现增删改查操作的方法详解
2019/05/13 PHP
JS+JSP checkBox 全选具体实现
2014/01/02 Javascript
AngularJS的一些基本样式初窥
2015/07/27 Javascript
js实现的后台左侧管理菜单代码
2015/09/11 Javascript
vue中如何创建多个ueditor实例教程
2017/11/14 Javascript
jQuery利用FormData上传文件实现批量上传
2018/12/04 jQuery
vue.js中使用echarts实现数据动态刷新功能
2019/04/16 Javascript
vue component 中引入less文件报错 Module build failed
2019/04/17 Javascript
vue-iview动态新增和删除的方法
2020/06/17 Javascript
python正则分组的应用
2013/11/10 Python
python求解水仙花数的方法
2015/05/11 Python
python妹子图简单爬虫实例
2015/07/07 Python
python去除字符串中的换行符
2017/10/11 Python
pip安装Python库时遇到的问题及解决方法
2017/11/23 Python
用TensorFlow实现lasso回归和岭回归算法的示例
2018/05/02 Python
Django实现简单网页弹出警告代码
2019/11/15 Python
django迁移文件migrations的实现
2020/03/31 Python
python 动态绘制爱心的示例
2020/09/27 Python
Scrapy爬虫文件批量运行的实现
2020/09/30 Python
pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异
2021/02/25 Python
CSS3之边框多颜色Border-color属性使用示例
2013/10/11 HTML / CSS
使用CSS3的背景渐变Text Gradient 创建文字颜色渐变
2014/08/19 HTML / CSS
ShellScript面试题一则-ShellScript编程
2014/06/24 面试题
建筑安全生产目标责任书
2014/07/23 职场文书
党的群众路线教育实践活动批评与自我批评范文
2014/10/16 职场文书
2015年教师节演讲稿范文
2015/03/19 职场文书
2015年底工作总结范文
2015/05/15 职场文书
太行山上观后感
2015/06/05 职场文书
初中物理教学反思
2016/02/19 职场文书
世界文化遗产导游词
2019/08/07 职场文书
高中班主任工作总结(范文)
2019/08/20 职场文书
成本低的5个创业项目:投资小、赚钱快
2019/08/20 职场文书
python3 实现mysql数据库连接池的示例代码
2021/04/17 Python