简体   繁体   English

关于使用 Python 的 webbot 的 Selenium 版本的错误

[英]Error about Selenium version using Python's webbot

I'm using Python 3.9 on macOS.我在 macOS 上使用 Python 3.9。 I'm trying to start using webbot, but every time I try, I get this error:我正在尝试开始使用 webbot,但每次尝试时,我都会收到此错误:

selenium.common.exceptions.SessionNotCreatedException: Message: session not created 
exception: Missing or invalid capabilities
  (Driver info: chromedriver=2.39.562713 
(dd642283e958a93ebf6891600db055f1f1b4f3b2),platform=Mac OS X 10.14.6 x86_64)

I'm using macOS version 10.4 because I use 32 bit software.我使用的是 macOS 版本 10.4,因为我使用的是 32 位软件。 The part that really puzzles me is why is says chromedriver=2.39.562713 .真正让我困惑的部分是为什么说chromedriver=2.39.562713 According to the pip, the driver's version is 103.0.5060.53.根据pip,驱动的版本是103.0.5060.53。 If I import selenium and try the command help(selenium) , towards the end of the output, I get:如果我导入selenium并尝试命令help(selenium) ,在输出结束时,我得到:

VERSION
    4.3.0

Where does this lower version come from?这个低版本是从哪里来的? I'm pretty sure that's why I have "missing or invalid capabilities."我很确定这就是为什么我有“缺失或无效的能力”。 If I start selenium with:如果我开始硒:

from selenium import webdriver
driver = webdriver.Chrome()

It starts Chrome as expected.它按预期启动 Chrome。 Obviously I'm missing something.显然我错过了一些东西。

I used to start webbot with:我曾经用以下方式启动webbot:

from webbot import Browser
driver = Browser()

But then, just to be sure, I changed it to:但是,为了确定起见,我将其更改为:

from webbot import Browser
driver = Browser(True, None, '/usr/local/bin/')

'/usr/local/bin/' being the location of a chrome webdriver installed by brew that is explicitly version 103. No difference. '/usr/local/bin/' 是 brew 安装的 chrome webdriver 的位置,它明确是 103 版。没有区别。

Solution解决方案

The approved response was not the solution, but it led me to the solution.批准的响应不是解决方案,但它使我找到了解决方案。

My version of webbot is the latest, but it has a very different __init__ method:我的webbot版本是最新的,但它有一个非常不同的__init__方法:

    def __init__(self, showWindow=True, proxy=None , downloadPath:str=None):

Upon further inspection, I saw that the driverPath attribute (that I had tried to use earlier) was completely gone by design.经过进一步检查,我发现driverPath属性(我之前尝试使用的)完全被设计为消失了。 So I decided to print the value of the inner variable driverpath inside the __init__ method.所以我决定在__init__方法中打印内部变量driverpath的值。 This returned the following:这返回了以下内容:

project_root/virtualenvironment/lib/python3.9/site-packages/webbot/drivers/chrome_mac

There was my guilty party!有我的罪人! I renamed that executable and in its place put a symbolic link to the correct binary.我重命名了该可执行文件,并在其位置放置了指向正确二进制文件的符号链接。 That worked.那行得通。

driver = Browser(True, None, '/usr/local/bin/')

actually sets the downloadPath , not the driverPath .实际上设置的是downloadPath ,而不是driverPath Use the parameter name explicitly显式使用参数名称

driver = Browser(driverPath='/usr/local/bin/')

From webbot.py来自webbot.py

class Browser:
    def __init__(self, showWindow=True, proxy=None , downloadPath:str=None, driverPath:str=None, arguments=["--disable-dev-shm-usage","--no-sandbox"]):

        if driverPath is not None and isinstance(driverPath,str):
            driverPath = os.path.abspath(driverPath)
            if(not os.path.isdir(driverPath)):
                raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), driverPath)

        if driverPath is None:
            driverfilename = ''
            if sys.platform == 'linux' or sys.platform == 'linux2':
                driverfilename = 'chrome_linux'
            elif sys.platform == 'win32':
                driverfilename = 'chrome_windows.exe'
            elif sys.platform == 'darwin':
                driverfilename = 'chrome_mac'
            driverPath = os.path.join(os.path.split(__file__)[0], 'drivers{0}{1}'.format(os.path.sep, driverfilename))
        
        self.driver = webdriver.Chrome(executable_path=driverPath, options=options)

If driverPath is None it will set to /{parent_folder_abs_path}/drivers/chrome_mac or /{parent_folder_abs_path}/drivers/ , I'm guessing you have an older chromedriver version there.如果driverPathNone它将设置为/{parent_folder_abs_path}/drivers/chrome_mac/{parent_folder_abs_path}/drivers/ ,我猜你那里有一个旧的chromedriver版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM