Using Selenium how to get network request

We Are Going To Discuss About Using Selenium how to get network request. So lets Start this Java Article.

Using Selenium how to get network request

Advertisements
  1. Using Selenium how to get network request

    Not exactly open by dev tools but found some network, performance and other results.
    Yes you can do that using JavascriptExecutor

  2. Using Selenium how to get network request

    Not exactly open by dev tools but found some network, performance and other results.
    Yes you can do that using JavascriptExecutor

Solution 1

Advertisements

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 🙂

Original Author Shubham Jain Of This Content

Solution 2

Advertisements

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

Original Author Norayr Sargsyan Of This Content

Solution 3

Advertisements
  1. You can use “browsermob-proxy”, “LoggingPreferences”, “CloseableHttpClient”, “HttpURLConnection” for getting the logs
  2. If you wish not to use browser and want to get the response, then I would suggest to go for “CloseableHttpClient”.
  3. Copy the URI (“www.somewebsite.com/v1/api/sign-in?”).
    Get the request payload(which will be available in that particular API URI). Pass all the parameters with “&” like this “www.somewebsite.com/v1/api/sign-in?&username=xyz&password=1234566&app_id=12123214324234134&app_secret=213242345345345”
    (Remember app id and app secret is very unique and do not expose it anywhere)
  4. Once you get the URI, this code will give you JSON format response
            HttpPost request = new HttpPost(str);
            request.setHeader("content-type", "application/json");
            HttpResponse response = client.execute(request);
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            while ((line = bufReader.readLine()) != null) {
                builder=String.valueOf(line);
            }

            System.out.println(builder);
        }

Original Author naveen kumar Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment