You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Selenium WebDriver捕获Chrome浏览器控制台日志错误

Capture Chrome Console Errors with Selenium WebDriver

Got it, let's put together a complete solution to capture Chrome's console errors using Selenium WebDriver and TestNG. I'll include the full working code and break down the key parts so you know exactly what's going on.

Complete Implementation Code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.logging.Level;

public class ChromeConsoleErrorCapture {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set up Chrome logging preferences
        LoggingPreferences loggingPrefs = new LoggingPreferences();
        // Capture browser logs (this includes the console) at ERROR level
        loggingPrefs.enable(LogType.BROWSER, Level.SEVERE);

        // Attach logging preferences to Chrome capabilities
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        caps.setCapability(CapabilityType.LOGGING_PREFS, loggingPrefs);

        // Initialize ChromeDriver with the configured capabilities
        driver = new ChromeDriver(caps);
    }

    @Test
    public void captureConsoleErrors() {
        // Navigate to your target page (replace with your URL)
        driver.get("https://your-target-url.com");

        // Retrieve all browser logs after page load/interaction
        LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);

        // Iterate through logs and filter for errors
        System.out.println("Captured Console Errors:");
        for (LogEntry entry : logEntries) {
            // Log level SEVERE corresponds to console errors
            if (entry.getLevel() == Level.SEVERE) {
                System.out.printf("Timestamp: %s | Level: %s | Message: %s%n",
                        entry.getTimestamp(), entry.getLevel(), entry.getMessage());
            }
        }
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Key Details to Note

  • Logging Preferences: We use LoggingPreferences to enable LogType.BROWSER logs at Level.SEVERE—this targets the browser's console errors. You can adjust the level to Level.WARNING if you want to capture warnings too, but SEVERE focuses strictly on errors.
  • DesiredCapabilities: This is how we pass the logging configuration to ChromeDriver. Without this, Selenium won't capture any console logs by default.
  • Retrieving Logs: After interacting with the page, we fetch the logs using driver.manage().logs().get(LogType.BROWSER). Make sure to do this after the actions that would generate console errors (like page load or user interactions).
  • Log Entry Details: Each LogEntry gives you the timestamp, log level, and the actual error message from the console—perfect for debugging.

Troubleshooting Tips

  • If you're not seeing any logs, double-check that your ChromeDriver version matches your installed Chrome browser version. Mismatched versions can cause logging issues.
  • Some frameworks or pages might log errors under different levels—if you're missing some, try switching to Level.ALL temporarily to see all logs, then narrow it down.
  • Ensure you're using the correct LogType: LogType.BROWSER is for the browser's console, while LogType.DRIVER is for WebDriver's own logs.

内容的提问来源于stack exchange,提问作者Chiranjib Prasadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:10:26