• 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版本命令

    1. # 谷歌浏览器版本查看                  
    2. google-chrome -version 
    3. # 对应的chromedriver版本查看
    4. chromedriver -version

            2.2、报错

    1. Traceback (most recent call last):
    2. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    3. stdin=PIPE)
    4. File "/root/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
    5. restore_signals, start_new_session)
    6. File "/root/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    7. raise child_exception_type(errno_num, err_msg, err_filename)
    8. FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
    9. During handling of the above exception, another exception occurred:
    10. Traceback (most recent call last):
    11. File "selenium获取chrome的pid.py", line 6, in
    12. driver = webdriver.Chrome(chrome_options=options)
    13. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    14. self.service.start()
    15. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    16. os.path.basename(self.path), self.start_error_message)
    17. 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、报错

    1. Traceback (most recent call last):
    2. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    3. stdin=PIPE)
    4. File "/root/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
    5. restore_signals, start_new_session)
    6. File "/root/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
    7. raise child_exception_type(errno_num, err_msg, err_filename)
    8. PermissionError: [Errno 13] Permission denied: '/root/chromedriver'
    9. During handling of the above exception, another exception occurred:
    10. Traceback (most recent call last):
    11. File "selenium获取chrome的pid.py", line 5, in
    12. driver = webdriver.Chrome(chrome_options=options,executable_path='/root/chromedriver')
    13. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    14. self.service.start()
    15. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 88, in start
    16. os.path.basename(self.path), self.start_error_message)
    17. 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、报错

    1. Traceback (most recent call last):
    2. File "/home/selenium获取chrome的pid.py", line 7, in
    3. driver = webdriver.Chrome(chrome_options=options)
    4. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    5. desired_capabilities=desired_capabilities)
    6. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    7. self.start_session(capabilities, browser_profile)
    8. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    9. response = self.execute(Command.NEW_SESSION, parameters)
    10. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    11. self.error_handler.check_response(response)
    12. File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    13. raise exception_class(message, screen, stacktrace)
    14. selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
    15. (unknown error: DevToolsActivePort file doesn't exist)
    16. (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不支持可视化则会报错。

    1. from selenium import webdriver
    2. options = webdriver.ChromeOptions()
    3. options.add_argument('--headless')
    4. options.add_argument('--disable-gpu')
    5. 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占满了,导致无响应。

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

                    

  • 相关阅读:
    Sentinel黑白名单授权规则解读
    div中的两个元素怎么实现上下排列
    43. 字符串相乘
    Docker - 镜像、容器、仓库
    使用并查集实现查找无向图的连通分量和求解有向图的强连通分量
    使用grabit分析mysql数据库中的数据血缘关系
    C语言从文件 D://test.txt 读取字符串,将字符串中所有的大写字符改为小写字母并写回到源文件中
    Java常用类(二)
    Flask四种配置方式
    【数据结构】栈和队列
  • 原文地址:https://blog.csdn.net/zkkkkkkkkkkkkk/article/details/128078527