我制作了一个python脚本,该脚本可抓取信息并单击一些按钮,并且在chrome webdriver上运行良好,但是当我尝试以无头方式输入它时,它在几秒钟内什么也不做,然后给出了错误。我的代码在下面。
options = Options()
options.headless = True
options.add_argument("user-data-dir=selenium")
browser = webdriver.Chrome(executable_path=r'C:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.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:\Users\REDACTED\Desktop\REDACTED\browser\chromedriver.exe', options=options)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.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)
答案 0 :(得分:0)
当我将硒与铬一起使用时,对我有用的是:
您可以尝试options.add_argument('--headless')
,因为您的方法会引发错误(仅适用于chrome,不适用Firefox)
您可以尝试的另一件事是从.exe
中删除executable_path
。当我添加.exe
时,也会遇到与您相同的错误。