How to fix selenium “DevToolsActivePort file doesn't exist” exception in Python [duplicate]

跟風遠走 提交于 2020-02-29 04:13:51

问题


When I use both arguments --headless and user-data-dir. Selenium raise selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist exception. If only 1 of them is used, then everything works as needs.

I tried to swap arguments and remove some of them. Specified the full path to chromedriver.exe. None of this helped.

chromeOptions.add_argument("--disable-dev-shm-using") DIDN'T HELP ME.

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() 

回答1:


I solve it by adding an argument --remote-debugging-port=<port>

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("--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("--headless") 
chromeOptions.add_argument(r"user-data-dir=.\cookies\\" + login) 
b = webdriver.Chrome(chrome_options=chromeOptions) 
b.get("https://google.com/") 
b.quit()


来源:https://stackoverflow.com/questions/56637973/how-to-fix-selenium-devtoolsactiveport-file-doesnt-exist-exception-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!