如何使用 selenium 和 Python 从浏览器控制台获取错误和警告?

时间:2021-03-02 13:15:03

标签: python selenium

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.

2 个答案:

答案 0 :(得分:0)

基本上它会查找 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)

答案 1 :(得分:0)

我知道一个很好的做法:“导入记录器”,但我会建议一个解决方案

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
    ])