Skip to content
Advertisement

Using Selenium how to get network request [closed]

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

This is my code:

ChromeOptions options = new ChromeOptions();
// Setting some chrome features here

ProxyServer proxyServer = new ProxyServer(4444);
proxyServer.start();

Proxy proxy = proxyServer.seleniumProxy();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new ChromeDriver(capabilities); // Error happens here

Network Request

Advertisement

Answer

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

Yes you can do that using JavascriptExecutor

Below Code will give you all performance, network etc entries:-

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 Below Code will give you specific performance entries:-

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

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

The second code return perfromance

Hope it will help you 🙂

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement