我在macOS上使用Python 3.9。我试图开始使用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)
我用的是macOS 10.4版本,因为我用的是32位软件。真正让我困惑的是为什么是chromedriver=2.39.562713
。根据pip,驱动程序的版本为103.0.5060.53。如果我导入selenium
并尝试命令help(selenium)
,在输出的末尾,我得到:
VERSION
4.3.0
这个较低的版本是从哪里来的?我很确定这就是为什么我有"缺失或无效的能力"。如果我以:
开始seleniumfrom selenium import webdriver
driver = webdriver.Chrome()
按预期启动Chrome。很明显我漏掉了什么。
webbot的开头是:
from webbot import Browser
driver = Browser()
但是,为了确保,我把它改成:
from webbot import Browser
driver = Browser(True, None, '/usr/local/bin/')
'/usr/local/bin/'是浏览器安装的浏览器驱动的位置,明确是103版本。没有区别。
解决方案被批准的响应不是解决方案,但它使我找到了解决方案。
我的webbot
版本是最新的,但它有一个非常不同的__init__
方法:
def __init__(self, showWindow=True, proxy=None , downloadPath:str=None):
进一步检查后,我看到driverPath
属性(我之前试图使用的)完全没有被设计。因此,我决定在__init__
方法中打印内部变量driverpath
的值。返回如下内容:
project_root/virtualenvironment/lib/python3.9/site-packages/webbot/drivers/chrome_mac
那是我有罪的一方!我重命名了该可执行文件,并在其位置放置了指向正确二进制文件的符号链接。这工作。
driver = Browser(True, None, '/usr/local/bin/')
实际上设置的是downloadPath
,而不是driverPath
。显式使用参数名
driver = Browser(driverPath='/usr/local/bin/')
从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)
如果driverPath
是None
,它将设置为/{parent_folder_abs_path}/drivers/chrome_mac
或/{parent_folder_abs_path}/drivers/
,我猜你有一个较旧的chromedriver
版本。