如何使用selenium和java捕获浏览器控制台日志

时间:2017-05-05 06:42:14

标签: javascript java selenium selenium-webdriver

我想捕获任何js事件触发器或错误的日志使用selenium和java.Any类型的帮助或建议将不胜感激?我尝试了这段代码,但它无法正常工作

public void HomePageConsole () throws InterruptedException {
    driver.findElement(By.id("drop-down")).click();
    driver.findElement(By.xpath(".//*[@id='js-top-currency']/li[4]/a")).click();
       LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
        for (LogEntry entry : logEntries) {
          // System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
           //System.out.println("Checking ExitUnit: 5th Line will be true ");
            Thread.sleep(10000);
            System.out.println("Exit Unit Open : "+entry.getMessage().contains("has been triggered!"));

}}

2 个答案:

答案 0 :(得分:2)

public void analyzeLog() {

    LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
    for (LogEntry entry : logEntries) {
        System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
        //do something useful with the data
    }
}

答案 1 :(得分:0)

通过它自己的 toString 覆盖打印 LogEntry 的一种更有效的方法:

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
    System.out.println(entry.toString());
}