加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

早上在linux下用selenium启动Chrome时出现问题:

发布时间:2020-12-14 02:00:02 所属栏目:Linux 来源:网络整理
导读:早上在linux下用selenium启动Chrome时出现问题: 报错: Traceback (most recent call last): File "get2.py",line 62,in module browser = webdriver.Chrome() File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdrive

早上在linux下用selenium启动Chrome时出现问题:
报错:

Traceback (most recent call last):
File "get2.py",line 62,in <module>
browser = webdriver.Chrome()
File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py",line 75,in __init__
desired_capabilities=desired_capabilities)
File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py",line 154,in __init__
self.start_session(desired_capabilities,browser_profile)
File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py",line 243,in start_session
response = self.execute(Command.NEW_SESSION,parameters)
File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py",line 311,in execute
self.error_handler.check_response(response)
File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py",line 237,in check_response
raise exception_class(message,screen,stacktrace)
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/google-chrome is no longer running,so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.9.0-6-amd64 x86_64)

解决方案:
加上代码:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument(‘--no-sandbox‘)
chrome_options.add_argument(‘--disable-dev-shm-usage‘)
chrome_options.add_argument(‘--headless‘)
browser = webdriver.Chrome(chrome_options=chrome_options)

其中
“–no-sandbox”参数是让Chrome在root权限下跑
“–headless”参数是不用打开图形界面
可以额外加这些参数获得更好体验

chrome_options.add_argument(‘blink-settings=imagesEnabled=false‘)
chrome_options.add_argument(‘--disable-gpu‘)
---------------------
原文:https://blog.csdn.net/zhangpeterx/article/details/83502641

?

?

系统版本:

??????? Red Hat 4.8.2-16

一、首先安装phantomjs

??????? 1)、下载程序文件

wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2

???????????? (注意:32位ubuntu下载链接是https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-i686.tar.bz2)

???????? 2)、解压文件

tar -xvf phantomjs-1.9.7-linux-x86_64.tar.bz2

???????? 注意:有些可能还需要修改合适的地址和名字? 改不改都行 不是必要因素。
二、配置环境变量

????????? 方法一:(全局环境变量)

????????????????? vi? /etc/profile
????????????????????? ? 加入以下内容:
??????????????????????????? export PATH=$PATH:/root/phantomjs/bin??? (红色字体为安装目录)
??????????????????????????? PATH=$PATH:$HOME/bin
??????????????????????????? export PATH
???????????????? 这里要执行 source /etc/profile,以另刚才的改动生效。

????????? 方法二:(用户环境变量)

????????????????? vi? .bash_profile

??????????????????????? 在export PATH上添加以下内容:????????
????????????????????????????? export PATH=/root/phantomjs/bin/phantomjs:$PATH? (红色字体为安装目录)

三、命令行输入

????????????????? phantomjs --version 如果能看到版本说明OK了? (注意 是? - - version 中间没有空格)

?

如果报错如下::

selenium.common.exceptions.WebDriverException: Message: Service /Users/Me/.npm-packages/bin/phantomjs unexpectedly exited. Status code was: 127

安装依赖包fontconfig 命令如下: yum --skip-broken install fontconfig freetype libfreetype.so.6 libfontconfig.so.1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读