linux下使用selenium调用谷歌浏览器的一些问题

目录

一、linux版本的谷歌浏览器问题

二、对应版本的chromedriver驱动问题

        2.1、查看谷歌及chromedriver版本命令

        2.2、报错

        2.3、解决

三、chromedriver权限问题

        3.1、报错

        3.2、解决

四、代码启动参数设置问题

        4.1、报错

        4.2、解决


一、linux版本的谷歌浏览器问题

        这部分可以下载官方的安装包(注意谷歌依赖的安装,这部分很繁琐),也可以进入博主的资源下载。

二、对应版本的chromedriver驱动问题

        2.1、查看谷歌及chromedriver版本命令

# 谷歌浏览器版本查看                  
google-chrome -version  

# 对应的chromedriver版本查看
chromedriver -version

        2.2、报错

Traceback (most recent call last):
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/root/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/root/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "selenium获取chrome的pid.py", line 6, in 
    driver = webdriver.Chrome(chrome_options=options)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

        2.3、解决

                版本对应的下载,参考  谷歌chromedriver对应版本下载  文章 或 chromedriver download

        下载完成后可以选择设置成环境变量,也可以选择在代码中手动配置chromedriver的路径( /usr/bin/ )。各自选择各自方便的方式即可。        

三、chromedriver权限问题

        3.1、报错

Traceback (most recent call last):
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/root/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/root/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/root/chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "selenium获取chrome的pid.py", line 5, in 
    driver = webdriver.Chrome(chrome_options=options,executable_path='/root/chromedriver')
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 88, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

        3.2、解决

                chmod 777 chromedriver        (执行命令的前提是进入到chromedriver的路径中)

四、代码启动参数设置问题

        4.1、报错

Traceback (most recent call last):
  File "/home/selenium获取chrome的pid.py", line 7, in 
    driver = webdriver.Chrome(chrome_options=options)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, 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.)

        4.2、解决

                --headless的这一行是要加的。不加--headless谷歌会启动浏览器窗口,如果你的linux不支持可视化则会报错。

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')

五、无法从渲染器获取,超时:300.00

selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000

        运行环境及版本情况

                linux

                python :3.6.3

                selenium :3.141.0

                google-chrome :106.0.5249.103

                chromedriver :106.0.5249.61

        关于这个报错,网上什么说法都有。大概就以下几点

                添加chrome启动参数,如: --disable-gpu

                还有讲因为截图(screet-shot)的问题

                或是因为google-chrome和chromedriver启动时把cpu占满了,导致无响应。

        我的错误奇怪的是它不常出现,属于偶然性情况。不知道哪天就会抛这个异常,很烦,而且我发现一个现象。我的程序只要出现这个异常,然后进程就被停止了。目前因为这个问题难复现,所以也不知道用了网上的方法能不能解决。

                

你可能感兴趣的:(Linux,python,爬虫,selenium,python,爬虫,chrome)