python selenium chrome不同错误的冲突解决方案 [英] Conflicting solutions for different errors with python selenium chrome

查看:62
本文介绍了python selenium chrome不同错误的冲突解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一周左右的时间里,我遇到了一系列令人讨厌的 Selenium/Chrome 错误,一段时间内一切正常,然后突然就不行了.我有适合我的 google chrome 版本 (89.0.4389.114) per google chrome 的 chromedriver(89.0.4389.23) 版本官方文档

I have been getting a series of pesky Selenium/Chrome errors for a week or so, where everything works fine for a while and then suddenly doesn't. I have the right version of chromedriver(89.0.4389.23) for my version of google chrome (89.0.4389.114) per google chrome official docs

目前,当我运行 driver = webdriver.Chrome(options=options) 时出现以下错误(至少在我的生产服务器上,我没有为相同的代码存储库获取它)在我的本地机器上):

Currently I'm getting the following error when I run driver = webdriver.Chrome(options=options) (at least on my production server, I don't get it for the same code repo on my local machine):

selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
  (Session info: headless chrome=89.0.4389.114)

令人沮丧的是,除了确保您的 chrome 和 chromedriver 匹配"之外,我发现的唯一一个解决方案;是从我的 webdriver options 中删除 options.add_argument("--remote-debugging-port=9222").我也有那行代码,现在删除它似乎可以解决我的问题,但是我的代码库中的以下注释下出现了我的问题:

Frustratingly, the only solution I've found for this error aside from "make sure your chrome and chromedriver match" is to remove options.add_argument("--remote-debugging-port=9222") from my webdriver options. I do have that line of code as well, and removing it appears to solve my problem for now, but my problem that line of code appears under the following comments in my codebase:

    # adding arg below fixed "DevToolsActivePort file doesn't exist."
    # ht https://github.com/heroku/heroku-buildpack-google-chrome/issues/46
    options.add_argument("--remote-debugging-port=9222")

所以我现在已经达到了一个完美的循环,我的 python selenium 代码可靠地工作,直到它可靠地中断,出现两个不同的 selenium 错误中的任何一个(会话未创建从断开连接:无法连接到渲染器"或DevToolsActivePort 文件不存在"),其中一个的解决方案是添加特定的代码行,另一个的解决方案是删除它.

So I've now reached a perfect circle where my python selenium code reliably works until it reliably breaks, with either of two different selenium errors("session not created from disconnected: unable to connect to renderer" or "DevToolsActivePort file doesn't exist."), where the solution to one is to add a specific line of code and the solution to the other is to remove it.

以下是我当前的 launch_webdriver 函数,其中的注释反映了我一直在尝试导航的各种瞬态 chrome/selenium 错误:

Below is my current launch_webdriver function, with comments reflecting the various transient chrome/selenium errors I've been trying to navigate:

from selenium import webdriver
import chromedriver_binary
from selenium.webdriver.chrome.options import Options

def launch_browser(headless=False):  # :, driverpath="./spinocchio/chromedriver"):
    options = Options()

    # if any other driver `options` are added, they must be added BELOW no-sandbox
    # per user parsecer's comment at https://stackoverflow.com/a/53073789/1870832
    # and my own excruciating experience.
    options.add_argument("--no-sandbox")

    # you may think the two lines below can be replaced with just options.headless=headless
    # please don't.
    if headless:
        options.add_argument("headless")

    # adding arg below fixed "DevToolsActivePort file doesn't exist."
    # ht https://github.com/heroku/heroku-buildpack-google-chrome/issues/46
    options.add_argument("--remote-debugging-port=9222")
    driver = webdriver.Chrome(options=options)
    return driver

任何有关获得更可靠/更强大的东西的帮助将不胜感激.

Any help on getting to something more reliable/robust would be much appreciated.

推荐答案

概述

  1. 普遍问题 - 根据 @ss7777
  2. 尽管导致此/相关的潜在原因很多

蓝色天空和银弹"解决方案我很担心,但是,这里有一些常见的原因和潜在的解决方案/注意事项等.

Blue-skies and 'silver-bullet' solution I'm afraid, however, here are some common causes and potential resolve / considerations etc.

修复/解决

(伴随着共同性"的主旨观点|频率)

(accompanied by subj. view of 'commonality' | frequency)

  1. 驱动程序:ChromeDriver 版本/文件位置 (~50%) - 不正确版本 + 确保位置到可执行正确位置

当您使用的二进制文件版本不兼容时,这个问题非常明显."(改编自 这个 来源,包含历史版本v/二进制文件的有用摘要)

"This issue is pretty evident when there is a incompatibility between the version of the binaries you are using." (adapted from this source, with useful summary of historical versions v/ binaries)

  1. Chrome/驱动程序仍然活跃"5-20% - 尽管关闭,进程树元素可以保持活跃"或旋转"一段时间 - 您是否在 PC 重新启动后立即注意到这一点(这里)?

(之前已经遭受过甚至证明了这一点 - 例如并行运行代码,或者只是通过 .bat 与 PyCharm 控制台同时执行).

(Have suffered and even evidenced this previously - e.g. running code in parallel, or simply executing through .bat at same time as PyCharm console).

  1. 更新:插件/其他不兼容(约 5-15%) - 注意到您使用的是无头 - 它可以与头一起使用吗?)
  2. 系统:清理您的系统/还原点或最坏的一面(~10-20%).(恢复点对我有用)
  3. 其他:生成单独的线程(~5% - 请参阅此处)/替代端口(根据要求/可行)
  1. Updates: plugin/other incompatibly (~5-15%) - noting you're using headless - does it work with head?)
  2. System: clean up your system/restore-point or worst-face (~10-20%). (restore point worked for me)
  3. Other: spawn separate thread (~5% - see here)/alternative ports (as req/feasible)


解决方法

  • Chrome 依赖项(例如,请参阅 此处 - 尽管有潜在的红鲱鱼......)
  • 执行模式(通过编辑器主程序,通过编辑器托管的 CMD 界面(例如 PyCharm 控制台),或通过 .bat 文件(如前所述),独立 Selenium 这里
  • 针对高级用户的配置(技术/高级 - 例如此处))立>
  • Chrome dependency (e.g. see here - albeit potential red-herring..)
  • Mode of execution (through editor main program, through CMD interface hosted via editor (e.g. PyCharm console), or through .bat file (as mentioned previously), Standalone Selenium here
  • Configuration (technical/advanced - e.g. here)for advanced users)

未解决(尽管有承诺)

这种 Q 仍未解决/贡献有限的几种情况:

Several cases of where such a Q remains unresolved/limited contribution:

(虽然这里的第 2 点是指上面第 2 项中的补救措施).

(although 2nd point here points to remedy in item 2 above).

极端情况:

  • 经历过激烈的调查和许多过往的问答 - 最终结论是完全重新安装(Chrome/python、编辑器)等.

链接/相关

这篇关于python selenium chrome不同错误的冲突解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆