如何在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 相关文章推荐
Windows8下安装Python的BeautifulSoup
Jan 22 Python
python中的闭包用法实例详解
May 05 Python
Python语言描述连续子数组的最大和
Jan 04 Python
如何用python整理附件
May 13 Python
python检测主机的连通性并记录到文件的实例
Jun 21 Python
用pycharm开发django项目示例代码
Oct 24 Python
python实现银联支付和支付宝支付接入
May 07 Python
Django-Model数据库操作(增删改查、连表结构)详解
Jul 17 Python
Python 函数绘图及函数图像微分与积分
Nov 20 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
Nov 21 Python
python 爬虫爬取京东ps4售卖情况
Dec 18 Python
python 远程执行命令的详细代码
Feb 15 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 反斜杠处理函数addslashes()和stripslashes()实例详解
2016/12/25 PHP
PHP页面静态化――纯静态与伪静态用法详解
2020/06/05 PHP
简略的前端架构心得&&基于editor为例子的编码小技巧
2010/11/25 Javascript
javascript学习(二)javascript常见问题总结
2013/01/02 Javascript
Extjs4中Form的使用之本地hiddenfield
2013/11/26 Javascript
js阻止默认事件与js阻止事件冒泡示例分享 js阻止冒泡事件
2014/01/27 Javascript
javascript操纵OGNL标签示例代码
2014/06/16 Javascript
jQuery 回调函数(callback)的使用和基础
2015/02/26 Javascript
jquery+css3实现网页背景花瓣随机飘落特效
2015/08/17 Javascript
AngularJS  自定义指令详解及实例代码
2016/09/14 Javascript
详解微信小程序——自定义圆形进度条
2016/12/29 Javascript
webpack项目轻松混用css module的方法
2018/06/12 Javascript
详解Vue中watch的详细用法
2018/11/28 Javascript
js实现黑白div块画空心的图形
2018/12/13 Javascript
微信小程序自定义toast组件的方法详解【含动画】
2019/05/11 Javascript
vue 解决addRoutes多次添加路由重复的操作
2020/08/04 Javascript
python操作字典类型的常用方法(推荐)
2016/05/16 Python
Python 备份程序代码实现
2017/03/06 Python
Python新手入门最容易犯的错误总结
2017/04/24 Python
python+tkinter编写电脑桌面放大镜程序实例代码
2018/01/16 Python
详解python while 函数及while和for的区别
2018/09/07 Python
Python面向对象之类和对象实例详解
2018/12/10 Python
浅谈python requests 的put, post 请求参数的问题
2019/01/02 Python
python ---lambda匿名函数介绍
2019/03/13 Python
Python线上环境使用日志的及配置文件
2019/07/28 Python
Python timer定时器两种常用方法解析
2020/01/20 Python
pytorch简介
2020/11/11 Python
Johnston & Murphy官网: 约翰斯顿·墨菲牛津总统鞋
2018/01/09 全球购物
FitFlop美国官网:英国符合人体工学的鞋类品牌
2018/10/05 全球购物
保证书范文大全
2014/04/28 职场文书
2014最新实习证明模板
2014/10/02 职场文书
优秀班主任材料
2014/12/16 职场文书
天鹅湖观后感
2015/06/09 职场文书
绿里奇迹观后感
2015/06/15 职场文书
学子宴致辞大全
2015/07/27 职场文书
Java无向树分析 实现最小高度树
2022/04/09 Javascript