selenium+python自动化100-centos上搭建selenium启动chrome浏览器headless无界面模式
环境准备
前言
selenium在windows机器上运行,每次会启动界面,运行很不稳定。于是想到用chrome来了的headless无界面模式,确实方便了不少。
为了提高自动化运行的效率和稳定性,于是把selenium自动化环境部署到linux服务器上,这样更方便。
环境:
centons 7.6
python 3.6
chrome 77.0.3865.90
chromedriver 77.0.3865.40
selenium 3.14
安装最新版chrome
方法一:下载到本地后安装
先下载google-chrome最新版77.0.3865.90(有些小伙伴可能下载不了,所以我放到QQ群750815713,群文件下载)
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
下载完成后,使用yum安装本地包
yum localinstall google-chrome-stable_current_x86_64.rpm
方法二:yum在线安装
yum install google-chrome-stable_current_x86_64.rpm
或者指定地址
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
安装完成之后,检查下版本号
> google-chrome -version
Google Chrome 77.0.3865.90
chromedriver驱动
下载chromedriver驱动,历史版本http://npm.taobao.org/mirrors/chromedriver找到对应的驱动版本
可以使用wget下载zip包
wget http://npm.taobao.org/mirrors/chromedriver/77.0.3865.40/chromedriver_linux64.zip
解压zip包,如果提示没有zip,那就yum -y install zip
先安装下
unzip chromedriver_linux64.zip # 解压zip
解压后把chromedriver移动到/usr/bin/目录下
mv chromedriver /usr/bin/
查看chromedriver版本号
> chromedriver --version
ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442})
安装selenium
安装最新版selenium 3.141.0
pip3 install selenium
[root@yoyo chrome]# pip show selenium
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: /usr/local/python3/lib/python3.6/site-packages
Requires: urllib3
Required-by:
[root@yoyo chrome]#
运行selenium代码
新建一个test_demo.py文件,运行测试代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless') # 无界面
chrome_options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在报错问题
chrome_options.add_argument('--disable-gpu') # 禁用GPU硬件加速。如果软件渲染器没有就位,则GPU进程将不会启动。
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--window-size=1920,1080') # 设置当前窗口的宽度和高度
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
#driver = webdriver.Chrome()
driver.get("https://www.cnblogs.com/yoyoketang/")
print(driver.page_source)
driver.quit()
运行代码
python3 test_demo.py
页面出现“上海-悠悠”博客首页的内容,上面运行成功了
Copyright © 2019 上海-悠悠
<br><span id="poweredby">Powered by .NET Core 3.0.0-preview9-19423-09 on Linux</span>
</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
</body></html>
linux上运行selenium效率会提升很多哟!
selenium+python自动化100-centos上搭建selenium启动chrome浏览器headless无界面模式的更多相关文章
- selenium+python自动化77-autoit文件上传
前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...
- selenium+python自动化77-autoit文件上传【转载】
前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...
- selenium + python 环境配置 (三)之启动chrome
安装启动chromedriver的方法和ie类似 2.启动chrome 即selenium调用ChromeDriver打开Chrome浏览器 ①下载并解压,你会得到一个chromedriver.exe ...
- selenium+python自动化85-python3.6上SendKeys报错用PyUserInput取代
前言 python2上安装SendKeys库,对于不好定位的元素,用快捷键操作是极好的,那么在3.6上安装时,会报错 python3.6安装SendKeys报错 1.python3.6安装SendKe ...
- selenium+python自动化98--文件下载弹窗处理(PyKeyboard)
前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...
- selenium+python自动化79-文件下载(SendKeys)
前言 文件下载时候会弹出一个下载选项框,这个弹框是定位不到的,有些元素注定定位不到也没关系,就当没有鼠标,我们可以通过键盘的快捷键完成操作. SendKeys库是专业的处理键盘事件的,所以这里需要用S ...
- Selenium2+python自动化75-非input文件上传(SendKeys)
前言 不少小伙伴问非input标签如何上传文档,这个本身就是一坑,无奈很多小伙伴非要跳坑里去,那就介绍一个非主流的上传文件方法吧,用第三方库SendKeys. 只支持python2环境 python3 ...
- linux无界面模式安装selenium+chrome+chromedriver并成功完成脚本(亲测可用)
环境:docker centos 7.4 能通外网 写好的selenium脚本. 具体步骤: 一:安装selenium 这是最简单的 直接利用 pip3 install selenium 二 安装c ...
- selenium+python自动化105 - selenium 如何在已打开的浏览器上继续运行自动化脚本?
前言 使用selenium 做web自动化的时候,经常会遇到这样一种需求,是否可以在已经打开的浏览器基础上继续运行自动化脚本? 这样前面的验证码登录可以手工点过去,后面页面使用脚本继续执行,这样可以解 ...
随机推荐
- centos 7 下 rabbitmq 3.8.0 & erlang 22.1 源码编译安装
centos 7 下 rabbitmq 3.8.0 & erlang 22.1 源码编译安装 安装前请检查好erlang和rabbitmq版本是否相匹配参考:RabbitMQ Erlang V ...
- [转帖]华为PC端台式机电脑来啦!自研主板及自研CPU处理器
华为PC端台式机电脑来啦!自研主板及自研CPU处理器 在性能上,4核版相当于酷睿i5 ,8核版相当于酷睿i5 8300H. https://www.bilibili.com/read/cv376376 ...
- GoCN每日新闻(2019-09-23)
1. 查看 Go 的代码优化过程http://xargin.com/go-compiler-opt 2. go 学习笔记之仅仅需要一个示例就能讲清楚什么闭包 https://segmentfault. ...
- mybatis-plus-generator 模板生成代码
maven: <dependencies> <dependency> <groupId>com.baomidou</groupId> <artif ...
- 【题解】子序列个数 [51nod1202] [FZU2129]
[题解]子序列个数 [51nod1202] [FZU2129] 传送门:子序列个数 \([51nod1202]\) \([FZU2129]\) [题目描述] 对于给出长度为 \(n\) 的一个序列 \ ...
- Oracel 数据库表操作
表结构操作 创建表 create table tableName (id varchar2(36) primary key, name varchar2(36), age number(12,2), ...
- 关于vs无法创建虚拟目录的问题
插入链接:https://blog.csdn.net/zhao1955/article/details/92182935 补充:改完之后不要忘记以管理员的身份运行vs
- c#十进制转换
1.方法定义 /// <summary> /// 十进制转换 /// </summary> /// <param name="hexChar"> ...
- C#控制操控操作多个UVC摄像头设备
有时,我们需要在C#代码中对多个UVC摄像头进行操作,如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像头控制项目 ...
- 多态性 类(class)的四则运算
我们知道c语言中可以整型数据或浮点型等做四则运算,而自己写的类也可做四则运算,是不是感觉奇怪,可以看以下代码是如何完成类之间的四则运算: #include "stdafx.h" ...