使用 Selenium 从 Firefox 获取 console.log 输出。
我们能够通过 Selenium 从 Firefox 中获取 console.log 输出。这是用 setProperty 方法完成的。FirefoxDriver.SystemProperty.BROWSER_LOGFILE 和将日志记录在其中的文件路径作为参数传递给该方法。
语法
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
刷新项目文件夹后,我们将获得要捕获其中的日志的 FFLogs.txt 文件。
示例
代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.logging.LogType;
public class FirefoxLogs{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
// set the System Property to BROWSER_LOGFILE and path of log file
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");
WebDriver driver = new FirefoxDriver();
driver.get("https://tutorialspoint.com/tutor_connect/index.php");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify element
driver.findElement(By.id("txtSearchText")).sendKeys("Selenium");
driver.quit();
}
}输出
在项目文件夹中创建一个名为 FFLogs.txt 的日志文件。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP