使用 Selenium 如何获取网络请求 [英] Using Selenium how to get network request

查看:223
本文介绍了使用 Selenium 如何获取网络请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 selenium 处理所有网络请求..我找不到任何方法来找到这个解决方案.如果有人可以建议我或提供代码或库,我们将不胜感激.

I want to take all the network request using selenium..I am not getting any way to find this solution.If anyone can suggest me or provide code or library that will be appreciated.

推荐答案

未完全通过开发工具打开,但发现了一些网络、性能和其他结果.

Not exactly open by dev tools but found some network, performance and other results.

是的,您可以使用 JavascriptExecutor 来做到这一点

Yes you can do that using 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());
}

第一个代码重跑网络return network;" 因为有这个JS标签,可以去掉不需要的实体的JS代码

The first code retrun network return network;" because of this JS tag. You can remove JS code of entity which you don't require

第二个代码返回性能

希望对你有帮助:)

这篇关于使用 Selenium 如何获取网络请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆