WebDriverException:消息:未知错误:DevTosActivePort文件不存在使用ChromeDriver Chrome和User-Data-dir时出错

lx0bsm1f  于 2022-09-18  发布在  Java
关注(0)|答案(4)|浏览(234)

我试图用不同的帐户访问一些网站,并避免每次我计划为每个网站创建用户配置文件时都登录。我在Ubuntu服务器上托管我的Python应用程序,所以我必须无头运行它,而且我还需要移动仿真。以下是我正在尝试做的一个示例:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_experimental_option("mobileEmulation", {"deviceName": "Pixel 2"})
options.add_argument("user-data-dir=account-data/account_3")
chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)

print("START SEARCH")

# Google is an example, but nothing works

driver.get("https://www.google.com")
print("FINISHED")

我看到了START SEARCH,但从未看到FINISHED。然而,当我注解user-data-dir选项时,一切都运行得很完美。奇怪的是,这发生在Ubuntu 20 LTS服务器上,但在我的Mac上没有问题。

在试图获得这个问题的版本时,突然出现了一个不起作用的版本,它给了我这样的信息:

Traceback (most recent call last):
  File "seleniumtest.py", line 12, in <module>
    driver = webdriver.Chrome(executable_path=chromedriver, options=options)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/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.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.4.0-1009-kvm x86_64)

在收到这个错误之前,我唯一真正做过的事情就是删除所有的/tmp/.org.chromium.Chromium.*文件,但这些文件应该只是临时文件,当我没有指定user-data-dir标志时,应该一直创建新的文件。

amrnrhlw

amrnrhlw1#

原来问题是我没有在user-data-dirlag中放入一个绝对路径。

from selenium import webdriver
import os

options = webdriver.ChromeOptions()
path = os.path.abspath("account-data/account_3")
options.add_argument("user-data-dir=" + path)

# other options...

chromedriver = "./webdrivers/chrome"
driver = webdriver.Chrome(executable_path=chromedriver, options=options)

# do stuff...

driver.close()
o75abkj4

o75abkj42#

I don't see any major issue in your code block.

However**chromedriver=2.41.578700**is pretty old and ancient.

Solution

Ensure that:

  • Selenium is upgraded to current levels Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v83.0 level.
  • Chrome is updated to current Chrome Version 83.0 level. (as per ChromeDriver v83.0 release notes)
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Execute your @Test asnon-rootuser.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
ocebsuys

ocebsuys3#

与其尝试所有代码,不如先下载chromeDriver并安装它。然后将这条路径放入下面的行中。

请记住,当您执行下面的脚本时,将打开一个新的Chrome窗口,但不会关闭它。

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\Users\sonal\.wdm\drivers\chromedriver\80.0.3987.106\win32\chromedriver.exe')
driver.get("https://www.google.com")
1zmg4dgp

1zmg4dgp4#

对于Mac用户(M1、M2)核心-

使用seleniarm/standalone-chromium而不是selenium/standalone-chrome

相关问题