【问题标题】:How to fix selenium "DevToolsActivePort file doesn't exist" exception in Python [duplicate]如何修复 Python 中的 selenium“DevToolsActivePort 文件不存在”异常 [重复]
【发布时间】:2019-10-31 10:34:30
【问题描述】:

当我同时使用 --headlessuser-data-dir 两个参数时。 Selenium 引发 selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist 异常。如果只使用其中之一,那么一切都按需要进行。

我尝试交换参数并删除其中一些。 指定 chromedriver.exe 的完整路径。 这些都没有帮助。

chromeOptions.add_argument("--disable-dev-shm-using") 没有帮助。

login = "test"
chromeOptions = webdriver.ChromeOptions() 
chromeOptions.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) 
chromeOptions.add_argument("--no-sandbox") 
chromeOptions.add_argument("--disable-setuid-sandbox") 
chromeOptions.add_argument("--disable-dev-shm-using") 
chromeOptions.add_argument("--disable-extensions") 
chromeOptions.add_argument("--disable-gpu") 
chromeOptions.add_argument("start-maximized") 
chromeOptions.add_argument("disable-infobars") 
chromeOptions.add_argument("--headless") 
chromeOptions.add_argument(r"user-data-dir=.\cookies\\" + login) 
b = webdriver.Chrome(chrome_options=chromeOptions) 
b.get("https://google.com/") 
b.quit() 

【问题讨论】:

    标签: python selenium selenium-chromedriver


    【解决方案1】:

    我通过添加参数--remote-debugging-port=<port>来解决它

    chromeOptions = webdriver.ChromeOptions() 
    chromeOptions.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) 
    chromeOptions.add_argument("--no-sandbox") 
    chromeOptions.add_argument("--disable-setuid-sandbox") 
    
    chromeOptions.add_argument("--remote-debugging-port=9222")  # this
    
    chromeOptions.add_argument("--disable-dev-shm-using") 
    chromeOptions.add_argument("--disable-extensions") 
    chromeOptions.add_argument("--disable-gpu") 
    chromeOptions.add_argument("start-maximized") 
    chromeOptions.add_argument("disable-infobars")
    chromeOptions.add_argument(r"user-data-dir=.\cookies\\test") 
    
    b = webdriver.Chrome(chrome_options=chromeOptions) 
    b.get("https://google.com/") 
    b.quit()
    

    【讨论】:

    • 您能解释一下为什么会解决这个问题吗?
    • @mosaad 所以,我忘记了一切,但我认为这可以解决问题,因为 chrome 无法连接到调试,您需要手动进行。
    • 您能说一下这段代码是在哪里添加的吗?文件名是什么?
    猜你喜欢
    • 2020-03-25
    • 1970-01-01
    • 2019-05-15
    • 2019-11-06
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    相关资源
    最近更新 更多