前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux 安装 chromedriver,selenium 踩坑总结

linux 安装 chromedriver,selenium 踩坑总结

原创
作者头像
shirishiyue
修改2019-03-25 13:10:25
30.3K2
修改2019-03-25 13:10:25
举报
文章被收录于专栏:全栈码

1、腾讯云服务器

2、Linux VM_0_10_centos 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

待运行python脚本:

代码语言:txt
复制
from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument('headless')
browser = webdriver.Chrome(chrome_options=option)

browser.get('http://www.baidu.com/')
print(browser.title)
exit();

安装详细过程:

代码语言:txt
复制
cd /usr/local/share
wget -N http://chromedriver.storage.googleapis.com/2.26/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

yum install python-pip

pip install selenium

执行,python脚本,报错:

代码语言:txt
复制
[root@VM_0_10_centos pythonproj]# python webdriver.py 
Traceback (most recent call last):
  File "webdriver.py", line 3, in <module>
    browser = webdriver.Chrome()
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/usr/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

手动查看 chromedriver 版本

代码语言:txt
复制
[root@VM_0_10_centos pythonproj]# chromedriver --version
chromedriver: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory

安装chrome浏览器:

代码语言:txt
复制
yum install chromium

执行,还是报错:

代码语言:txt
复制
[root@VM_0_10_centos ~]# chromedriver
chromedriver: error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory

搜索,发现这个so文件问题,是少了这个库GConf2,http://landcareweb.com/questions/25967/que-shao-gong-xiang-ku-libgconf-2-so-4

安装这个库:

代码语言:txt
复制
yum install GConf2

再执行 webdriver,搞定!

代码语言:txt
复制
[root@VM_0_10_centos ~]# chromedriver
Starting ChromeDriver 2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac) on port 9515
Only local connections are allowed.

再执行pathon脚本,报错:

代码语言:txt
复制
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64)

查看版本问题:

代码语言:txt
复制
[root@VM_0_10_centos pythonproj]# yum list installed | grep chro
    chromium.x86_64                     71.0.3578.98-2.el7             @epel
[root@VM_0_10_centos pythonproj]# chromedriver --version
    ChromeDriver 2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac)

好像是版本不一致的问题,看这个:https://www.jianshu.com/p/40027de48c5b

ChromeDriver的版本太低了,跟安装的浏览器版本搭不上。

重新安装下 ChromeDriver版本 :v2.45

代码语言:txt
复制
cd /usr/local/share
wget -N http://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

再执行脚本,依旧报错:

代码语言:txt
复制
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64)

不过,这次错误明显好很多。

搜一下,找到解决办法:https://github.com/heroku/heroku-buildpack-google-chrome/issues/46

代码语言:txt
复制
Don't think it's has a heroku issue. Had a similar issue with docker container as well. Here is my fix that worked in docker -

`
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/path/to/your_chrome_driver_dir/chromedriver',chrome_options=chrome_options)

修改python脚本如下:

代码语言:txt
复制
from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
browser = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=option)

browser.get('http://www.baidu.com/')
print(browser.title)
exit();

再执行,成功!

运行一段时候后,又报错:

代码语言:txt
复制
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
  (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64)

查看进程:

代码语言:txt
复制
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# ps -ef | grep chrome
root      2947     1  0 12:51 ?        00:00:00 /usr/local/bin/chromedriver --port=40245
root      3615     1  0 12:55 ?        00:00:00 /usr/local/bin/chromedriver --port=45273
root      5809     1  0 13:10 pts/0    00:00:01 /usr/local/bin/chromedriver --port=44337
root      8637  5268  0 13:30 pts/0    00:00:00 grep --color=auto chrome
root     21378     1  0 11:11 ?        00:00:01 /usr/local/bin/chromedriver --port=47677
root     25024     1  0 Mar18 ?        00:00:00 /usr/local/bin/chromedriver --port=38096
root     25346     1  0 Mar18 ?        00:00:00 /usr/local/bin/chromedriver --port=42048
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# killall chromedriver
[root@VM_0_10_centos ~]# ps -ef | grep chrome
root      8674  5268  0 13:31 pts/0    00:00:00 grep --color=auto chrome

不知道是不是这影响,,,代码里面没有browser.quit(),如上,kill chromedriver 掉再试试。

还是报错,继续查,

代码语言:txt
复制
[root@VM_0_10_centos ~]# ps -ef | grep driver
root      2956     1  0 12:51 ?        00:00:02 /usr/bin/chromium-browser --enable-plugins --enable-extensions --enable-user-scripts --enable-printing --enable-gpu-rasterization --enable-sync --auto-ssl-client-auth --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/tmp/.org.chromium.Chromium.dteWvh/internal --log-level=0 --metrics-recording-only --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.org.chromium.Chromium.tOyVfN data:,
......  一大堆这个进程
代码语言:txt
复制
[root@VM_0_10_centos ~]# killall chromium-browser

再重试,好了。

代码语言:txt
复制
[root@VM_0_10_centos ~]# killall chromedriver
[root@VM_0_10_centos ~]# killall chromium-browser
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# 
[root@VM_0_10_centos ~]# ps -ef | grep chro
root     13735 13285  0 14:08 pts/0    00:00:00 grep --color=auto chro

脚本退出时,一定要主动调用 driver.quit !!!

修改脚本:

代码语言:txt
复制
from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
browser = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=option)

browser.get('http://www.baidu.com/')
print(browser.title)
browser.quit()            # 这个一定要加。加了就没上面的问题了。

附带一个应用例子源码:用Python写一个微信提醒备忘录,https://github.com/azhaochen/doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档