如何在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 相关文章推荐
PHP魔术方法__ISSET、__UNSET使用实例
Nov 25 Python
详解python发送各类邮件的主要方法
Dec 22 Python
解决python3中自定义wsgi函数,make_server函数报错的问题
Nov 21 Python
对python的bytes类型数据split分割切片方法
Dec 04 Python
Python实现网页截图(PyQT5)过程解析
Aug 12 Python
python多进程间通信代码实例
Sep 30 Python
python中对_init_的理解及实例解析
Oct 11 Python
Python新手如何进行闭包时绑定变量操作
May 29 Python
tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例
Jun 22 Python
Selenium及python实现滚动操作多种方法
Jul 21 Python
PyCharm2020.3.2安装超详细教程
Feb 08 Python
python实现调用摄像头并拍照发邮箱
Apr 27 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
相对路径转化成绝对路径
2007/04/10 PHP
php防止CC攻击代码 php防止网页频繁刷新
2015/12/21 PHP
浅谈PHP接入(第三方登录)QQ登录 OAuth2.0 过程中遇到的坑
2017/10/13 PHP
Dojo 学习要点
2010/09/03 Javascript
Javascript delete 引用类型对象
2013/11/01 Javascript
jquery马赛克拼接翻转效果代码分享
2015/08/24 Javascript
Bootstrap分页插件之Bootstrap Paginator实例详解
2016/10/15 Javascript
jQuery手指滑动轮播效果
2016/12/22 Javascript
vue 组件的封装之基于axios的ajax请求方法
2018/08/11 Javascript
通过微信公众平台获取公众号文章的方法示例
2019/12/25 Javascript
Vue切换div显示隐藏,多选,单选代码解析
2020/07/14 Javascript
tornado捕获和处理404错误的方法
2014/02/26 Python
pygame 精灵的行走及二段跳的实现方法(必看篇)
2017/07/10 Python
python中将一个全部为int的list 转化为str的list方法
2018/04/09 Python
python 创建一维的0向量实例
2019/12/02 Python
简单了解python元组tuple相关原理
2019/12/02 Python
关于Tensorflow分布式并行策略
2020/02/03 Python
python 等差数列末项计算方式
2020/05/03 Python
python绘制分布折线图的示例
2020/09/24 Python
在python中对于bool布尔值的取反操作
2020/12/11 Python
Melijoe时尚童装德国官网:Melijoe德国
2016/09/03 全球购物
党员个人思想汇报
2013/12/28 职场文书
高中体育教学反思
2014/01/24 职场文书
《植物妈妈有办法》教学反思
2014/02/25 职场文书
代理协议书范本
2014/04/22 职场文书
党员争先创优承诺书
2015/01/20 职场文书
小班下学期个人总结
2015/02/12 职场文书
工作服管理制度范本
2015/08/06 职场文书
中学教师教学工作总结
2015/08/13 职场文书
《当代神农氏》教学反思
2016/02/23 职场文书
Python 数据可视化之Matplotlib详解
2021/11/02 Python
浅谈mysql哪些情况会导致索引失效
2021/11/20 MySQL
关于MySQL临时表为什么可以重名的问题
2022/03/22 MySQL
python+pytest接口自动化之token关联登录的实现
2022/04/06 Python
ant design vue的form表单取值方法
2022/06/01 Vue.js
2022年显卡天梯图(6月更新)
2022/06/17 数码科技