selenium 浏览器常用设置和部署
一,chrome浏览器设置
from selenium import webdriver
# 浏览器选项
chrome_options = webdriver.ChromeOptions()
# 使用headless无界面浏览器模式
chrome_options.add_argument('--headless')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
chrome_options.add_argument('lang=zh_CN.UTF-8')
# 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('--hide-scrollbars')
# 禁止加载图片
chrome_options.add_argument('blink-settings=imagesEnabled=false')
# 指定浏览器分辨率
chrome_options.add_argument('window-size=1440x900')
# 设置默认请求头
chrome_options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
# 设置代理
desired_capabilities = chrome_options.to_capabilities()
desired_capabilities['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}
# 启动浏览器,获取网页源代码
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(url)
# 显示页面源码
html = driver.page_source
# 关闭当前页面
driver.close()
# 退出浏览器
driver.quit()
二,Firefox浏览器设置
from selenium import webdriver
# 浏览器选项
firefox_options = webdriver.FirefoxOptions()
# 使用headless无界面浏览器模式
firefox_options.add_argument('--headless')
# 谷歌文档提到需要加上这个属性来规避bug
firefox_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
firefox_options.add_argument('lang=zh_CN.UTF-8')
# 隐藏滚动条, 应对一些特殊页面
firefox_options.add_argument('--hide-scrollbars')
# 禁止加载图片
firefox_options.add_argument('blink-settings=imagesEnabled=false')
# 指定浏览器分辨率
firefox_options.add_argument('window-size=1440x900')
# driver.maximize_window()
# 设置默认请求头
firefox_options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
# 设置代理
desired_capabilities = firefox_options.to_capabilities()
desired_capabilities['proxy'] = {
"httpProxy":PROXY,
"ftpProxy":PROXY,
"sslProxy":PROXY,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
}
# 启动浏览器,获取网页源代码
driver = webdriver.Firefox(firefox_options=firefox_options)
driver.get(url)
# 显示页面源码
html = driver.page_source
# 关闭当前页面
driver.close()
# 退出浏览器
driver.quit()
三,部署
1.centos7无桌面环境部署
环境要求:
CentOS 7 Firefox 56.0+ Selenium 3.5+ geckodriver 0.19+
Xvfb(X virtual framebuffer)是一个虚拟显示服务器,不需要显示设备也能模拟运行图形界面
安装 Xvfb:
yum install xorg-x11-server-Xvfb bzip gtk3
安装火狐浏览器
cd /usr/local wget https://ftp.mozilla.org/pub/firefox/releases/56.0.2/linux-x86_64/en-US/firefox-56.0.2.tar.bz2 tar -jxvf firefox-56.0.2.tar.bz2 ln -s /usr/local/firefox/firefox /usr/bin/firefox rm -rf firefox-56.0.2.tar.bz2
安装selenium
pip3 install selenium
安装Firefoxdriver
wget https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz tar -zxvf geckodriver-*.tar.gz ln -s /root/geckodriver /usr/bin/geckodriver # 最新版本下载 https://github.com/mozilla/geckodriver/releases
运行测试:
1.启动虚拟桌面坏境,保持后台运行。
Xvfb :1 -screen 0 1024x768x24 & :1是服务启动的端口号,可以任意设置,与下一步保持一致就行
2.配置环境变量
export DISPLAY=:1 端口号和上面一致,冒号不能漏
3.启动程序测试
from selenium import webdriver
b = webdriver.Firefox(executable_path='/root/geckodriver')
b.get('http://www.baidu.com')
print(b.page_source)
b.quit()
1.3最新chromedriver安装
phantomjs逐步淡出我们的实现,已经不再被支持,chrome集高并发的优点,且目前也已支持无头浏览器。
安装chrome
curl https://intoli.com/install-google-chrome.sh | bash
测试
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.suning.com/ # 在当前路径下生成一张截图。
下载最新版本的chromedriver,
https://sites.google.com/a/chromium.org/chromedriver/downloads
# 解压 $ unzip chromedriver_linux64.zip # 测试 $ ./chromedriver # 加入环境变量
引入虚拟界面(实际没有这样做)
from pyvirtualdisplay import Display
display = Display(visible=0,size=(800,600))
display.start()
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
print(driver.page_source)
实际环境中采用的写法,并成功了.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
# 使用代理ip
chrome_options.add_argument("--proxy-server=http://202.20.16.82:10152")
# 使用headless无界面浏览器模式
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 禁止加载图片
chrome_options.add_argument('blink-settings=imagesEnabled=false')
# 设置默认请求头
chrome_options.add_argument("user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'")
wd = webdriver.Chrome(chrome_options=chrome_options,executable_path='/home/chrome/chromedriver')
wd.get("https://www.163.com")
content = wd.page_source.encode('utf-8')
print(content)
wd.quit()
selenium 浏览器常用设置和部署的更多相关文章
- linux云服务器常用设置
前面的话 由于在云服务器上配置自己的网站,将Linux里的常用设置记录如下 更改shell 默认地, ubuntu系统默认的shell是dash,但更常用的shell是bash 通过下面命令可以将da ...
- python3 selenium模块Chrome设置代理ip的实现
python3 selenium模块Chrome设置代理ip的实现 selenium模块Chrome设置代理ip的实现代码: from selenium import webdriver chrome ...
- Selenium浏览器自动化测试使用(2)
Selenium - 环境安装设置 为了开发Selenium RC或webdriver脚本,用户必须确保他们有初始配置完成.有很多关联建立环境的步骤.这里将通过详细的讲解. 下载并安装Java 下载并 ...
- 《Pro Express.js》学习笔记——Express框架常用设置项
Express 设置 系统设置 1. 无须再定义,大部分有默认值,可不设置 2. 常用设置 env view cache view engine views trust pro ...
- Eclipse下Tomcat常用设置
Eclipse下Tomcat常用设置 1,Eclipse建立Tomcat服务 1.1 新建Server 首先这里是指,jee版的Eclipse.Eclipse是没有像MyEclipse那样集成Tomc ...
- Python入门之PyCharm的快捷键与常用设置和扩展(Mac系统)
1. 快捷键 2 . PyCharm的常用设置和扩展 ------------------------------------------------------------------------- ...
- redis常用服务安装部署
常用服务安装部署 学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell 即将登场的是: mysql(m ...
- Eclipse 使用前常用设置
1.常用设置的位置 Eclipse中一般的设置都是在这个位置进行设置的: 2.设置字体类型和大小 一般可以设置成这样代码比较清晰:Consolas + 常规 + 小四 3.设置各种编码 设置工作空间的 ...
- HTML head标签内部常用设置
HTML head标签内部常用设置 在网页html文件中,head标签里面通常放置的代码是用来对网页进行相关设置的内容.下面就是对这些内容的介绍. meta标签的设置 在网页中,meta标签最常用的设 ...
随机推荐
- 小朋友学C语言(4):单精度浮点数与双精度浮点数
上节课简单介绍了浮点数.计算机程序中的浮点数分为单精度浮点数和双精度浮点数. 单精度和双精度精确的范围不一样. 计算机里的最基本的存储单位用位(bit)来表示.bit只能用来存储0或1. 稍大一点的单 ...
- JavaScript之函数,词法分析,内置对象和方法
函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别. // 普通函数定义 function f1() { console.log("Hello wo ...
- json.loads()的字符串中为单引号引发的错误
如下错误属于弱智错误,但是错的原因让我无语,所以记录一下 str2="{'card':6217001650004184441}"print(json.loads(str2)) Tr ...
- vue 整合element-ui
本文主要介绍如何在vue框架中结合elementUI. 本文主要参考: http://element-cn.eleme.io/#/zh-CN/component/quickstart 1.阅读本文 ...
- mac上使用jmeter录制web项目和手机app
前言: 最近熟悉jmeter进行带宽测试和并发测试,发现网上大多都是windows版本,自己用的mac,实验后发现大同小异 1.下载,我使用的jmeter3.2的版本,可以在网上下载,不区分mac版和 ...
- apt-get 常用命令总结
apt-get 高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian及其衍生发行版(如:ubuntu)的软件包管理器.APT可以自动下载,配置,安装二进 ...
- POJ2311 Cutting Game 博弈 SG函数
Cutting Game Description Urej loves to play various types of dull games. He usually asks other peopl ...
- 最全spring boot视频系列,你值得拥有
================================== 从零开始学Spring Boot视频 ================================== àSpringBoot ...
- 服务器搭建私人Git
环境是CentOS 7.4 64位 主要参考:在服务器上搭建 Git 0. 预备 安装git yum install git 1. 开发者-生成个人SSH公钥 p.s. 书中的4.3节是[生成个人的S ...
- 安装hyperledger fabric V1.0.1
安装文档位置: https://github.com/hyperledger/fabric fabric代码托管地址 https://hyperledger-fabric.readthedoc ...