如何在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 不关闭控制台的实现方法
Oct 23 Python
Python中使用tarfile压缩、解压tar归档文件示例
Apr 05 Python
Python中的super用法详解
May 28 Python
Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法
Feb 03 Python
python 通过xml获取测试节点和属性的实例
Mar 31 Python
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】
Dec 03 Python
python url 参数修改方法
Dec 26 Python
Python Opencv实现图像轮廓识别功能
Mar 23 Python
python 的 scapy库,实现网卡收发包的例子
Jul 23 Python
Python中url标签使用知识点总结
Jan 16 Python
Spark处理数据排序问题如何避免OOM
May 21 Python
Tensorflow之MNIST CNN实现并保存、加载模型
Jun 17 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
解析php5配置使用pdo
2013/07/03 PHP
PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
2016/12/24 PHP
javascript 定义新对象方法
2010/02/20 Javascript
在JavaScript中获取请求的URL参数
2010/12/22 Javascript
基于jquery的分页控件(C#)
2011/01/06 Javascript
你必须知道的JavaScript 变量命名规则详解
2013/05/07 Javascript
浅谈JS中的!=、== 、!==、===的用法和区别
2016/09/24 Javascript
vue按需加载组件webpack require.ensure的方法
2017/12/13 Javascript
vue 实现滚动到底部翻页效果(pc端)
2019/07/31 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
2016/04/13 Python
python中解析json格式文件的方法示例
2017/05/03 Python
Python实现的简单模板引擎功能示例
2017/09/02 Python
Python魔法方法功能与用法简介
2019/04/04 Python
python Tcp协议发送和接收信息的例子
2019/07/22 Python
python从PDF中提取数据的示例
2020/10/30 Python
python的数学算法函数及公式用法
2020/11/18 Python
俄罗斯女装店:12storeez
2019/10/25 全球购物
阿联酋手表和配饰购物网站:Rivolishop
2019/11/25 全球购物
巴西备受欢迎的服装和生活方式品牌:FARM Rio
2020/02/04 全球购物
会计大学生职业生涯规划书范文
2014/01/13 职场文书
毕业自我评价
2014/02/05 职场文书
公司中秋节活动方案
2014/02/12 职场文书
《水上飞机》教学反思
2014/04/10 职场文书
优秀党务工作者事迹材料
2014/05/07 职场文书
励志演讲稿800字
2014/08/21 职场文书
重阳节慰问信
2015/02/15 职场文书
大一学生个人总结
2015/02/15 职场文书
2015年度个人工作总结报告
2015/10/24 职场文书
《清澈的湖水》教学反思
2016/02/17 职场文书
《司马光》教学反思
2016/02/22 职场文书
24句精辟的现实社会语录,句句扎心,道尽人性
2019/08/29 职场文书
python实现三阶魔方还原的示例代码
2021/04/28 Python
React 高阶组件HOC用法归纳
2021/06/13 Javascript
详解Flask开发技巧之异常处理
2021/06/15 Python
MySQL中的引号和反引号的区别与用法详解
2021/10/24 MySQL