Selenium WebDriverException with headless chrome



我制作了一个python脚本,它可以抓取信息并点击几个按钮,它在chrome网络驱动程序上运行得很好,但当我尝试将其输入为无头时,它在几秒钟内什么都不做,然后出现错误。我的代码在下面。

options = Options()
options.headless = True
options.add_argument("user-data-dir=selenium")
browser = webdriver.Chrome(executable_path=r'C:UsersREDACTEDDesktopREDACTEDbrowserchromedriver.exe', options=options)
browser.get('https://REDACTED')

这是错误

Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/Test/REDACTED.py", line 49, in <module>
browser = webdriver.Chrome(executable_path=r'C:UsersREDACTEDDesktopREDACTEDbrowserchromedriver.exe', options=options)
File "C:UsersREDACTEDAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverchromewebdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:UsersREDACTEDAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:UsersREDACTEDAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:UsersREDACTEDAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersREDACTEDAppDataLocalProgramsPythonPython37libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
(Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)

当我使用硒和铬时,对我有效的是:

您可以尝试options.add_argument('--headless'),因为您的方法会出现错误(仅适用于chrome,不适用于firefox(

您可以尝试的另一件事是从executable_path中删除.exe。当我添加.exe时,我会得到与您相同的错误。

相关内容

  • 没有找到相关文章

最新更新