如何在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计算N天之后日期的方法
Mar 31 Python
python操作 hbase 数据的方法
Dec 18 Python
浅谈python中列表、字符串、字典的常用操作
Sep 19 Python
python基本语法练习实例
Sep 19 Python
python3实现全角和半角字符转换的方法示例
Sep 21 Python
python中is与双等于号“==”的区别示例详解
Nov 21 Python
PyQt5 对图片进行缩放的实例
Jun 18 Python
python Web flask 视图内容和模板实现代码
Aug 23 Python
对Tensorflow中Device实例的生成和管理详解
Feb 04 Python
keras中模型训练class_weight,sample_weight区别说明
May 23 Python
Django如何重置migration的几种情景
Feb 24 Python
python获取带有返回值的多线程
May 02 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判断输入是否是纯数字,英文,汉字的方法
2015/03/05 PHP
8个必备的PHP功能开发
2015/10/02 PHP
PHP递归的三种常用方式
2019/02/28 PHP
XHTML-Strict 内允许出现的标签
2006/12/11 Javascript
摘自启点的main.js
2008/04/20 Javascript
JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
2010/01/12 Javascript
JS中动态添加事件(绑定事件)的代码
2011/01/09 Javascript
Javascript中定义方法的另类写法(批量定义js对象的方法)
2011/02/25 Javascript
javascript中[]和{}对象使用介绍
2013/03/20 Javascript
Javascript 按位取反运算符 (~)
2014/02/04 Javascript
NodeJS学习笔记之Connect中间件模块(一)
2015/01/27 NodeJs
手机开发必备技巧:javascript及CSS功能代码分享
2015/05/25 Javascript
javascript父子页面通讯实例详解
2015/07/17 Javascript
JS实现微信弹出搜索框 多条件查询功能
2016/12/13 Javascript
jQuery插件FusionCharts实现的3D柱状图效果实例【附demo源码下载】
2017/03/03 Javascript
原生javascript移动端滑动banner效果
2017/03/10 Javascript
JS中Safari浏览器中的Date
2017/07/17 Javascript
web前端vue filter 过滤器
2018/01/12 Javascript
vue自定义移动端touch事件之点击、滑动、长按事件
2018/07/10 Javascript
Element Table的row-class-name无效与动态高亮显示选中行背景色
2018/11/30 Javascript
vue中使用echarts的示例
2021/01/03 Vue.js
[09:43]DOTA2每周TOP10 精彩击杀集锦vol.5
2014/06/25 DOTA
python刷投票的脚本实现代码
2014/11/08 Python
Python设计模式编程中Adapter适配器模式的使用实例
2016/03/02 Python
Python Django框架模板渲染功能示例
2019/11/08 Python
Python计算IV值的示例讲解
2020/02/28 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
面向游戏玩家和书呆子的极客订阅盒:Loot Crate
2020/11/25 全球购物
总裁办公室主任职责
2014/01/02 职场文书
活动邀请函范文
2014/01/19 职场文书
教师业务学习制度
2014/01/25 职场文书
招聘专员岗位职责
2014/03/07 职场文书
中国入世承诺
2014/04/01 职场文书
特种设备安全管理制度
2015/08/06 职场文书
导游词之京东大峡谷旅游区
2019/10/29 职场文书
一篇文章了解正则表达式的替换技巧
2022/02/24 Javascript