一,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 浏览器常用设置和部署的更多相关文章

  1. linux云服务器常用设置

    前面的话 由于在云服务器上配置自己的网站,将Linux里的常用设置记录如下 更改shell 默认地, ubuntu系统默认的shell是dash,但更常用的shell是bash 通过下面命令可以将da ...

  2. python3 selenium模块Chrome设置代理ip的实现

    python3 selenium模块Chrome设置代理ip的实现 selenium模块Chrome设置代理ip的实现代码: from selenium import webdriver chrome ...

  3. Selenium浏览器自动化测试使用(2)

    Selenium - 环境安装设置 为了开发Selenium RC或webdriver脚本,用户必须确保他们有初始配置完成.有很多关联建立环境的步骤.这里将通过详细的讲解. 下载并安装Java 下载并 ...

  4. 《Pro Express.js》学习笔记——Express框架常用设置项

    Express 设置 系统设置 1.       无须再定义,大部分有默认值,可不设置 2.       常用设置 env view cache view engine views trust pro ...

  5. Eclipse下Tomcat常用设置

    Eclipse下Tomcat常用设置 1,Eclipse建立Tomcat服务 1.1 新建Server 首先这里是指,jee版的Eclipse.Eclipse是没有像MyEclipse那样集成Tomc ...

  6. Python入门之PyCharm的快捷键与常用设置和扩展(Mac系统)

    1. 快捷键 2 . PyCharm的常用设置和扩展 ------------------------------------------------------------------------- ...

  7. redis常用服务安装部署

    常用服务安装部署   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell 即将登场的是: mysql(m ...

  8. Eclipse 使用前常用设置

    1.常用设置的位置 Eclipse中一般的设置都是在这个位置进行设置的: 2.设置字体类型和大小 一般可以设置成这样代码比较清晰:Consolas + 常规 + 小四 3.设置各种编码 设置工作空间的 ...

  9. HTML head标签内部常用设置

    HTML head标签内部常用设置 在网页html文件中,head标签里面通常放置的代码是用来对网页进行相关设置的内容.下面就是对这些内容的介绍. meta标签的设置 在网页中,meta标签最常用的设 ...

随机推荐

  1. 在django中,执行原始sql语句

    extra()方法 结果集修改器,一种提供额外查询参数的机制 使用extra: 1:Book.objects.filter(publisher__name='广东人员出版社').extra(where ...

  2. elasticsearch无故关闭,Log无报错

    可以看到图中的关闭log之前没有任务报错,这也让博主非常抓狂,这看着就像是人为关闭的,于是博主在群里问是不是有人动过该服务,确认没人关闭后,百度无果,社区上也没找到有关信息,最后灵光一闪,猜测是不是因 ...

  3. appium 搭建及实例

    一.Appium环境搭建(Java版本) 转载2016-04-26 09:24:55 标签:appium移动端自动化测试 市场需求与职业生涯的碰撞,阴差阳错我就跨进了移动App端自动化测试的大门,前生 ...

  4. is 和 == 区别,id() ,回顾编码,encode(),decode()

    1. is 和 == 区别 id()函数 == 判断两边的值 is 判断内存地址例 s = "alex 是 大 xx"# abc = id(s) # 得到内存地址# print(a ...

  5. mysql 动态拼接表字段,值 mybatis 动态获取表字段

    -- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...

  6. 使用Hexo + Github Pages搭建个人独立博客

    使用Hexo + Github Pages搭建个人独立博客 https://linghucong.js.org/2016/04/15/2016-04-15-hexo-github-pages-blog ...

  7. tkinter widget

    tkinter messagebox

  8. 如何在github上创建仓库,并将本地的文件上传到对应的远程仓库

    1.安装git,可从 http://www.bootcss.com/p/git-guide/下载git 2.在github上创建仓库,注意不勾选Initialize this repository w ...

  9. add_featurelayer_to_map

    var jsonFS = { "geometryType": "esriGeometryPolygon", "features": [ { ...

  10. zookeeper(1)初识zookeeper

    一.zookeeper的安装 1.下载zookeeper(当然在安装zookeeper之前得先装好jdk,这里就不说了),版本自己随便选一个(后面我再说版本的问题),点击这里下载. 2.然后在usr下 ...