How to fix selenium quot;DevToolsActivePort file doesn#39;t existquot; exception in Python(如何修复硒“DevToolsActivePort 文件不存在Python 中的异常)
问题描述
当我同时使用参数 --headless 和 user-data-dir 时.Selenium 引发 selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort 文件不存在 异常.如果只使用其中一个,那么一切都按需要进行.
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.
我尝试交换参数并删除其中一些.指定 chromedriver.exe 的完整路径.这些都没有帮助.
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") 没有帮助.
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()
推荐答案
我通过添加参数解决它 --remote-debugging-port=<port>
I solve it by adding an argument --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()
这篇关于如何修复硒“DevToolsActivePort 文件不存在"Python 中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何修复硒“DevToolsActivePort 文件不存在"Python 中的异常
基础教程推荐
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
