【报错】unknown error: DevToolsActivePort file doesn‘t exis

场景

在使用selenium 突然有天报错了,如题所示,代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()
option.add_argument('--headless')
option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage')

browser = webdriver.Chrome('./chromedriver',chrome_options=option)

browser.get('http://www.baidu.com/')
print(browser.title)
browser.quit()  

解决

首先要保证 google-chrome和chromedriver的版本是一致的,如果确定一致则跳过下面的三步。

  1. 查看google-chrome版本命令:

    google-chrome --version
    
  2. 查看chromedriver版本命令我还不太清楚,但是查到了google-chrome版本就可以去下面的网站下载对应的chromedriver了。

  3. 如果版本不一致,需要下载一致的版本,下载位置

    1. google-chrome:http://dist.control.lth.se/public/CentOS-7/x86_64/google.x86_64/
    2. chromedriver:https://sites.google.com/a/chromium.org/chromedriver/downloads

然后解决方案是,option添加一条

option.add_argument("--remote-debugging-port=9222")  # this

就成功啦!

参考:
[1] https://stackoverflow.com/questions/56637973/how-to-fix-selenium-devtoolsactiveport-file-doesnt-exist-exception-in-python


版权声明:本文为xiangduixuexi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。