Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Selenium how to get network request

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.

Network Request

like image 268
Zakaria Shahed Avatar asked Aug 23 '17 18:08

Zakaria Shahed


People also ask

How do I get to the network panel in Chrome developer tools using Selenium?

Download selenium language-specific client drivers from - http://docs.seleniumhq.org/download/ and add apropriate jar files to your project build path. To run a test with Chrome/Chromium you will also need chromdriver binary which you can download from - http://chromedriver.storage.googleapis.com/index.html.


2 Answers

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

Yes you can do that using JavascriptExecutor

Code is as below :-

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

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

like image 143
Shubham Jain Avatar answered Sep 20 '22 17:09

Shubham Jain


It's working for me

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());  } 
like image 24
Norayr Sargsyan Avatar answered Sep 18 '22 17:09

Norayr Sargsyan