通过selenium webdriver

时间:2018-03-30 10:15:09

标签: google-chrome selenium

我正在使用以下代码:

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;

public class ChromeConsoleLogging {

    private WebDriver driver;

    @BeforeMethod
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe");        
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        driver = new ChromeDriver(caps);
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

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

    @Test
    public void testMethod() {
        driver.get("http://mypage.com");
        //do something on page
        analyzeLog();
    }
}

但这一行:

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);

给出以下错误:

  

方法logs()未定义类型WebDriver.Options()

相同的代码正在运行,但现在它正在抛出错误。我该如何解决?

1 个答案:

答案 0 :(得分:0)

正如@Alexander所说,这条线中间不需要的字符串存在问题。我在我的系统中试过这个。它工作正常。请删除"在此处输入代码"然后再试一次。

import java.util.Date;
import java.util.logging.Level;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver; //avaiable
import org.openqa.selenium.chrome.ChromeDriver; //available
import org.openqa.selenium.logging.LogEntries;//yes
import org.openqa.selenium.logging.LogEntry;//yes
import org.openqa.selenium.logging.LogType;//yes
import org.openqa.selenium.logging.LoggingPreferences;//yes
import org.openqa.selenium.remote.CapabilityType;//yes
import org.openqa.selenium.remote.DesiredCapabilities;//yes
import org.testng.annotations.AfterMethod;//yes
import org.testng.annotations.BeforeMethod;//yes
import org.testng.annotations.Test;//yes

public class Test123 {  //give your class name


 private WebDriver driver;



@BeforeMethod
public void setUp() {
    System.setProperty("webdriver.chrome.driver", "G:\\Drivers\\chromedriverwin32\\chromedriver.exe");        
    DesiredCapabilities caps = DesiredCapabilities.chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.BROWSER, Level.ALL);
    caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    driver = new ChromeDriver(caps);
}

@AfterMethod
public void tearDown() {
    driver.quit();
}

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


@Test
 public  void testMethod() {
        driver.get("https://www.facebook.com");
        //do something on page
        analyzeLog();
    }


}