在使用 Selenium 进行数据处理的过程中,发现了一些问题:
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
首先确认我们的 ChromeDriver 和已经安装的 Chromium 的版本匹配没有问题
chromium-browser --version
Chromium 90.0.4430.212 Fedora Project
../../driver/chromedriver --version
ChromeDriver 89.0.4389.23 (61b08ee2c50024bab004e48d2b1b083cdbdac579-refs/branch-heads/4389@{#294})
但是,之后发现问题依然存在。
经历了大量的调查,应该是上次使用 webdriver 的时候,没有关闭浏览器,所以
[root@iz8vb7tuq6sw9mx44vk7p6z ~]# ps aux | grep Chrome
root 18561 0.0 0.0 112812 976 pts/5 R+ 08:26 0:00 grep --color=auto Chrome
scrapy 19401 0.0 1.7 34313044 17356 pts/2 Sl Nov02 0:12 /usr/bin/chromium-browser --enable-plugins --enable-extensions --enable-user-scripts --enable-printing --enable-gpu-rasterization --enable-sync --auto-ssl-client-auth --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --headless --log-level=0 --no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.bV7kKM data:,
scrapy 19671 0.0 1.3 34300716 13676 pts/2 Sl Nov02 0:11 /usr/bin/chromium-browser --enable-plugins --enable-extensions --enable-user-scripts --enable-printing --enable-gpu-rasterization --enable-sync --auto-ssl-client-auth --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --headless --log-level=0 --no-first-run --no-service-autorun --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.nm1tQ1 data:,
scrapy 31991 0.0 1.3 34300716 13328 pts/2 Sl Nov02 0:10 /usr/bin/chromium-browser --enable-plugins --enable-extensions --enable-user-scripts --enable-printing --enable-gpu-rasterization --enable-sync --auto-ssl-client-auth --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --headless --log-level=0 --no-first-run --no-sandbox --no-service-autorun --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.E71n9Z data:,
这里发现我们有3个 chromium 浏览器在运行,所以我们找到他们的 PID 并将他们一一关闭。
kill -9 PID
重新打开代码,就没有问题了