使用硒如何获取网络请求 [已关闭]
2022-09-01 13:09:50
不完全由开发工具打开,但发现了一些网络,性能和其他结果。
是的,你可以使用JavascriptExecutor做到这一点
下面的代码将为您提供所有性能,网络等条目:-
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
或以下代码将为您提供特定的性能条目:-
DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
System.out.println(le.getMessage());
}
第一个代码由于这个JS标签而重新运行网络。您可以删除不需要的实体的JS代码return network;"
第二个代码返回性能
希望它能帮助你:)
它为我工作
ChromeOptions options = new ChromeOptions();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable( LogType.PERFORMANCE, Level.ALL );
options.setCapability( "goog:loggingPrefs", logPrefs );
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
List<LogEntry> entries = driver.manage().logs().get(LogType.PERFORMANCE).getAll();
System.out.println(entries.size() + " " + LogType.PERFORMANCE + " log entries found");
for (LogEntry entry : entries) {
System.out.println(entry.getMessage());
}