注册 X
提交 注:点击提交后系统会发送邮件到邮箱验证!(仅支持中国大陆邮箱)
我已阅读并同意 服务条款
首页 > IT技术笔记 > 查看笔记

Centos安装chrome执行python自动化脚本

附件:

google-chrome-stable_current_x86_64.rpm

下载rpm安装包 the rpm pkg of google-stable

https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

dependencies

yum install redhat-lsb

yum install libXScrnSaver*

yum install epel-release

yum install liberation-fonts

安装 google-chrome

rpm -ivh google-chome-xxx

如果装过程中报错:

yum localinstall google-chrome-stable_current_x86_64.rpm

这样的话就会自己解决依赖问题,以前使用rpm安装谷歌地球的时候就由于没能解决依赖问题,这回可以再试试了


解决执行python时selenium报错--unknown error: DevToolsActivePort file doesn't exist

 

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”
参数是让Chromeroot权限下跑
“–headless”
参数是不用打开图形界面
可以额外加这些参数获得更好体验

chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--disable-gpu')


	        
			
	      

 打赏        分享



评论

邮箱: 昵称:
\n