如何在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中获取对象信息的方法
Apr 27 Python
给Python中的MySQLdb模块添加超时功能的教程
May 05 Python
python爬虫实战之最简单的网页爬虫教程
Aug 13 Python
Python中使用Counter进行字典创建以及key数量统计的方法
Jul 06 Python
python实现移位加密和解密
Mar 22 Python
Django 缓存配置Redis使用详解
Jul 23 Python
django框架创建应用操作示例
Sep 26 Python
Python网络编程之使用TCP方式传输文件操作示例
Nov 01 Python
pytorch 修改预训练model实例
Jan 18 Python
django的模型类管理器——数据库操作的封装详解
Apr 01 Python
Python unittest单元测试openpyxl实现过程解析
May 27 Python
python实现简单区块链结构
Apr 25 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中file_get_contents函数抓取https地址出错的解决方法(两种方法)
2015/09/22 PHP
php处理带有中文URL的方法
2016/07/11 PHP
php实现xml转换数组的方法示例
2017/02/03 PHP
浅谈thinkphp的nginx配置,以及重写隐藏index.php入口文件方法
2019/10/12 PHP
在线游戏大家来找茬II
2006/09/30 Javascript
JS时间选择器 兼容IE6,7,8,9
2012/06/26 Javascript
ExtJS实现文件下载的方法实例
2013/11/09 Javascript
JavaScript利用正则表达式去除日期中的-
2014/06/09 Javascript
jQuery知识点整理
2015/01/30 Javascript
jquery淡入淡出效果简单实例
2016/01/14 Javascript
Bootstrap表单组件教程详解
2016/04/26 Javascript
js css实现垂直方向自适应的三角提示菜单
2016/06/26 Javascript
基于原生js淡入淡出函数封装(兼容IE)
2016/10/20 Javascript
详解给Vue2路由导航钩子和axios拦截器做个封装
2018/04/10 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
解决vue中el-tab-pane切换的问题
2020/07/19 Javascript
Vue切换组件实现返回后不重置数据,保留历史设置操作
2020/07/21 Javascript
js 压缩图片的示例(只缩小体积,不更改图片尺寸)
2020/10/21 Javascript
javascript实现前端分页功能
2020/11/26 Javascript
[15:23]教你分分钟做大人:虚空假面
2014/10/30 DOTA
Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例
2017/08/31 Python
django项目运行因中文而乱码报错的几种情况解决
2017/11/07 Python
简单了解什么是神经网络
2017/12/23 Python
便捷提取python导入包的属性方法
2018/10/15 Python
linux环境下Django的安装配置详解
2019/07/22 Python
python处理RSTP视频流过程解析
2020/01/11 Python
基于python计算并显示日间、星期客流高峰
2020/05/07 Python
在django中form的label和verbose name的区别说明
2020/05/20 Python
对python pandas中 inplace 参数的理解
2020/06/27 Python
萌新HTML5 入门指南(二)
2020/11/09 HTML / CSS
EVE LOM英国官网:全世界最好的洁面膏
2017/10/30 全球购物
以特惠价提供在线奢侈品购物:FRMODA.com
2018/01/25 全球购物
三个儿子教学反思
2014/02/03 职场文书
项目总经理岗位职责
2014/02/14 职场文书
小学生国庆演讲稿
2014/09/05 职场文书
python和C/C++混合编程之使用ctypes调用 C/C++的dll
2022/04/29 Python