首页 > 解决方案 > 如何使用 selenium 和 Python 从浏览器控制台获取错误和警告?

问题描述

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }

driver = webdriver.Chrome(desired_capabilities=dc)
driver.implicitly_wait(30)

for entry in driver.get_log('browser'):
    print(entry)
 
driver.quit()

我运行上面的代码,但在 pycharm 中出现以下 2 个错误

in _execute_child    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

在处理上述异常的过程中,又出现了一个异常:

in start    raise WebDriverException(selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

标签: pythonselenium

解决方案


基本上它会查找 chrome webdriver 可执行文件,您可以在此处找到并下载它:https ://chromedriver.chromium.org/ 有两种解决方案:

  1. 将 chrome 可执行路径添加到系统 PATH 变量中(对于 Windows:https ://www.java.com/en/download/help/path.html )
  2. 在此行中手动添加 chromedriver 路径: driver = webdriver.Chrome(desired_capabilities=dc, executable_path = PATH_TO_WEBDRIVER)

推荐阅读