环境准备html
selenium在windows机器上运行,每次会启动界面,运行很不稳定。因而想到用chrome来了的headless无界面模式,确实方便了很多。 为了提升自动化运行的效率和稳定性,因而把selenium自动化环境部署到linux服务器上,这样更方便。 环境: centons 7.6 python 3.6 chrome 77.0.3865.90 chromedriver 77.0.3865.40 selenium 3.14python
方法一:下载到本地后安装linux
先下载google-chrome最新版77.0.3865.90(有些小伙伴可能下载不了,因此我放到QQ群750815713,群文件下载)git
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpmgithub
下载完成后,使用yum安装本地包web
yum localinstall google-chrome-stable_current_x86_64.rpmchrome
yum install google-chrome-stable_current_x86_64.rpmnpm
或者指定地址windows
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驱动,历史版本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 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]#
新建一个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效率会提高不少哟!