使用 Selenium 如何获取网络请求

新手上路,请多包涵

我想使用 selenium 处理所有网络请求。如果有人可以向我建议或提供将不胜感激的代码或库,我将无法找到此解决方案。

网络请求

原文由 Zakaria Shahed 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

不完全由开发工具打开,但发现了一些网络、性能和其他结果。

是的,您可以使用 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 代码

第二个代码返回性能

希望它能帮助你 :)

原文由 Shubham Jain 发布,翻译遵循 CC BY-SA 4.0 许可协议

它对我有用

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());
 }

原文由 Norayr Sargsyan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题