linux下安装chrome驱动和selenium
前言:
selenium在windows机器上运行,每次会启动界面,运行很不稳定。于是想到用chrome来了的headless无界面模式,确实方便了不少。
为了提高自动化运行的效率和稳定性,于是把selenium自动化环境部署到linux服务器上,这样更方便。
环境::
- centons 7.6
- python 3.6
- chrome 80.0.3978.149
- chromedriver 80.0.3978.149
- selenium 3.14
安装chrome
方法1:先下载到本地后安装
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
下载完成后,使用yum安装本地包
yum localinstall google-chrome-stable_current_x86_64.rpm
安装完成后,检查版本号:
[root@xiaoxiao ~]# google-chrome -version Google Chrome 80.0.3987.149
chromedriver驱动
下载chromedriver驱动,历史版本http://npm.taobao.org/mirrors/chromedriver找到对应的驱动版本
可以使用wget下载zip包
wget http://npm.taobao.org/mirrors/chromedriver/80.0.3987.106/chromedriver_linux64.zip
解压zip包,如果提示没有zip,那就yum -y install zip先安装
unzip chromedriver_linux64.zip
解压后把chromedriver移动到/usr/bin/目录下
mv chromedriver /usr/bin
查看chromedriver版本号
[root@xiaoxiao ~]# chromedriver --version ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882})
安装selenium
pip3 install selenium
[root@xiaoxiao ~]# pip3 show selenium Name: selenium Version: 2.53.6 Summary: Python bindings for Selenium Home-page: https://github.com/SeleniumHQ/selenium/ Author: UNKNOWN Author-email: UNKNOWN License: UNKNOWN Location: /root/python36/lib/python3.6/site-packages Requires: Required-by: Appium-Python-Client [root@xiaoxiao ~]#
测试运行selenium代码
python3 selenium_test.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/june-/") print(driver.page_source) driver.quit()
如果出现博客title,则运行成功。
[root@xiaoxiao ~]# python3 selenium_test.py <html lang="zh-cn"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="referrer" content="origin"> <meta http-equiv="Cache-Control" content="no-transform"> <meta http-equiv="Cache-Control" content="no-siteapp"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>做一只热爱生活的小透明 - 博客园</title> <link rel="stylesheet" href="/css/blog-common.min.css?v=BKtyzabbeYJEVOaELkxmRjHbp7LT-v37GzrU5S24bJk"> <link id="MainCss" rel="stylesheet" href="/skins/red_autumnal_leaves/bundle-red_autumnal_leaves.min.css?v=XYaHoXnfxIDko8oNhF5m3BYALnsNSBprp32AE1eRULc"> <link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/red_autumnal_leaves/bundle-red_autumnal_leaves-mobile.min.css?v=ADiCwO2hOTdd5yYidcx7eob7ix2VJI4o_TXjEycTHjs">
善于跌倒仍喜爱奔跑~

浙公网安备 33010602011771号