Java 使用Selenium如何获取网络请求
我想使用selenium处理所有网络请求。我没有找到任何方法来找到此解决方案。如果有人可以建议我或提供代码或库,我将不胜感激Java 使用Selenium如何获取网络请求,java,selenium-webdriver,webdriver,Java,Selenium Webdriver,Webdriver,我想使用selenium处理所有网络请求。我没有找到任何方法来找到此解决方案。如果有人可以建议我或提供代码或库,我将不胜感激 开发工具没有完全打开,但发现了一些网络、性能和其他结果 是的,您可以使用JavascriptExecutor来完成 代码如下:- ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); DesiredCapabilities capabilities = D
开发工具没有完全打开,但发现了一些网络、性能和其他结果 是的,您可以使用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);
或
第一个代码返回网络返回网络;“
因为这个JS标记。您可以删除您不需要的实体的JS代码
第二个代码返回PerfRoman
希望它能帮助你:)
HttpPost请求=新的HttpPost(str);
setHeader(“内容类型”、“应用程序/json”);
HttpResponse response=client.execute(请求);
BufferedReader bufReader=新的BufferedReader(新的InputStreamReader(
response.getEntity().getContent());
而((line=bufReader.readLine())!=null){
builder=String.valueOf(行);
}
System.out.println(生成器);
}
它对我有用
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());
}
ChromeOptions选项=新的ChromeOptions();
LoggingPreferences logPrefs=新的LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE,Level.ALL);
选项.setCapability(“goog:loggingPrefs”,logPrefs);
WebDriverManager.chromedriver().setup();
WebDriver=新的ChromeDriver(选项);
驱动程序。获取(“http://www.google.com");
列表条目=driver.manage().logs().get(LogType.PERFORMANCE).getAll();
System.out.println(entries.size()+“”+LogType.PERFORMANCE+“找到日志条目”);
用于(日志条目:条目){
System.out.println(entry.getMessage());
}
您是否在寻找这一点:?另一种方法-使用代理捕获所有网络活动,在快速搜索中,我发现:对于python和文档,第一个代码示例的输出不是有效的JSON(主要是缺少引号)。为了解决此问题并获得有效的JSON输出,请使用Javascript字符串中的return JSON.stringify(network)代替return network;
;
将输出带有引号和全部内容的有效JSON。感谢分享@George Pantazes…我将尝试尽快用Javascript解决方案更新它。通过性能
条目获取网络请求只会在页面加载时提供网络请求,并且无法轮询后续的异步/ajax调用。我相信日志记录解决方案也是如此,因为它也记录性能(尽管我还没有尝试过日志记录代码).@ShubhamJain为了我自己的教育,你能确认这两个代码段是否都得到性能请求吗?我想考虑到使用性能的局限性,我会改为使用。我尝试使用这种方法,但我认为它不是一个完整的日志。有什么方法可以拥有一个更完整的网络日志吗?例如,请求的类型,如果NSE是成功的…?browsermob代理已经有几年没有维护了。我们刚刚发布了一个名为的主动维护fork。它与browsermob代理具有相同的API,但还支持Java 11、HTTP/2、更新的依赖项以及关心它的人。
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());
}