2017-08-23 31 views
1

我想采取所有的网络请求使用硒..我没有得到任何方式来找到这个解决方案。如果任何人都可以建议我或提供代码或库,将不胜感激。使用硒如何获得网络请求

Network Request

+0

你在找这样的:HTTPS://www.openhub。 net/p/selenium-profiler? –

+0

另一种方式 - 使用代理捕获所有网络活动,在快速搜索中我找到了这个:https://github.com/lightbody/browsermob-proxy –

回答

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

OR

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

因为这样做的第一个代码retrun网络return network;" JS标签。您可以删除实体的JS代码,您不需要

第二个代码返回perfromance

希望它会帮助你:)