How can I capture network traffic of a specific page using Selenium

+1 vote
Jul 19, 2019 in Selenium by anonymous
22,413 views

1 answer to this question.

+2 votes

Hey, to capture network network traffic of a specific page using Selenium Webdriver:

public class CaptureNetworkTraffic {

public static WebDriver driver;
public static String driverPath = "C:\\\\Users\\\\Abha_Rathour\\\\Downloads\\\\ExtractedFiles\\\\chromedriver_win32\\\\chromedriver.exe";

@SuppressWarnings("deprecation")
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", driverPath);

DesiredCapabilities caps = DesiredCapabilities.chrome();

LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);

caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

driver= new ChromeDriver(caps);
driver.get("https://www.edureka.co/");

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(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());

}

driver.close();
driver.quit();

}
}

This will produce following output:



For further understanding, you can refer to the Selenium Course.

answered Jul 19, 2019 by Abha
• 28,140 points

hi my friend i use your code but it doesnt work how should i do?

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument: log type 'performance' not found

  (Session info: chrome=76.0.3809.87)

Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'

System info: host: 'DESKTOP-BOLOAH3', ip: '168.158.152.158', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_212'

Driver info: org.openqa.selenium.chrome.ChromeDriver

Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 76.0.3809.87, chrome: {chromedriverVersion: 76.0.3809.126 (d80a294506b4..., userDataDir: C:\Users\Mobin\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:1991}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}

Session ID: 31f02508307d36fbb666f0d4573cb6f2

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)

at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:35)

at org.openqa.selenium.remote.RemoteLogs.getRemoteEntries(RemoteLogs.java:81)

at org.openqa.selenium.remote.RemoteLogs.get(RemoteLogs.java:77)

at main.main(main.java:30)
Hi Mobin, did you use the exactly same code or you changed something in it.
Yeah Im Sure, Which Version of JDK and Selenium do you use ?
I am using Selenium 3.9.1 version and JDK of 11.0.2 version.

Hi! 

For anyone coming to this recently - in recent Selenium and ChromeDriver versions (eg 3.14.159, chrome driver 76.x) setting up loggingPrefs with ChromeDriver doesn't seem to work, using the local ChromeDriver - no matter what you do you seem to get the log type 'performance' not found error

The reason is increasingly strict w3c spec compliance, both in Selenium and ChromeDriver code.

Instead of the loggingPrefs/CapabilityType.LOGGING_PREFS capability, you need to use goog:loggingPrefs, like so

    ChromeOptions options = new ChromeOptions();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable( LogType.PERFORMANCE, Level.ALL );
    options.setCapability( "goog:loggingPrefs", logPrefs );

I guess that sooner or later the Selenium side will notice this and change the CapabilityType.LOGGING_PREFS constant or add a new one, but the project didn't look very friendly to log an issue with to me.

I solved this by following this thread: https://stackoverflow.com/questions/53049026/selenium-chrome-performance-logs-not-working

Hi,

Code works fine but log doesn't contain any information about ajax requests.Is there any possible way to include it? Thanks in advance.

Hello G3,

With this.driver.manage().logs().get(LogType.BROWSER).getAll() you should receive all logs that you can see in browser's console.

Related Questions In Selenium

0 votes
1 answer

How can I scroll a web page in Mozilla Firefox using Selenium?

Hi Rohan, steps to scroll a webpage in ...READ MORE

answered May 14, 2019 in Selenium by Pratibha
• 3,690 points
2,861 views
0 votes
1 answer

How can I locate web elements from a web page using Selenium Webdriver?

Hey @Dushyant, Locating elements in WebDriver is ...READ MORE

answered May 29, 2019 in Selenium by Shreya
2,597 views
0 votes
1 answer

How can I perform multiple selection of options in a dropdown using Select class in Selenium?

Hey Priyansh, you can select multiple options ...READ MORE

answered Jul 8, 2019 in Selenium by Anvi
• 14,150 points
2,907 views
0 votes
1 answer

How can I capture screenshot of a webpage partially in Selenium?

Hey Tejasvi, you can capture screenshot of ...READ MORE

answered Jul 8, 2019 in Selenium by Anvi
• 14,150 points
1,017 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,617 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,519 views
0 votes
1 answer

How can I get meta-description of a webpage using Selenium Webdriver?

Hey Sonal, to get the meta-description of ...READ MORE

answered Jul 23, 2019 in Selenium by Abha
• 28,140 points
5,307 views
0 votes
1 answer

How can I locate inline elements from a web page using Selenium Webdriver?

Which specific element you are trying to ...READ MORE

answered Oct 10, 2019 in Selenium by Abha
• 28,140 points
3,715 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP