简体   繁体   English

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

[英]How can i capture Browser console log using selenium and java

I want to capture log for any js event trigger or error Using selenium and java.Any type of help or suggestion will be appreciated?我想使用 selenium 和 java 捕获任何 js 事件触发器或错误的日志。任何类型的帮助或建议将不胜感激? I tried this code but it's not working properly我试过这段代码,但它不能正常工作

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!"));

}}
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
    }
}

A bit more efficient way to print the LogEntry by it's own toString override:通过它自己的 toString 覆盖打印 LogEntry 的一种更有效的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM