我如何得到错误和警告从浏览器控制台使用硒和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.

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

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

我知道一个很好的做法:"import logger"但我将建议一个解决方案

import time
time_at_run_script=time.time()

driver.get('https://httpbin.org')
# ... code script ...

print([
entry
for entry in driver.get_log("browser")
if entry["timestamp"] > time_at_run_script
])  

最新更新