How to get browser network logs using python selenium












1















I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.



And I'm using selenium 3.14.0 and latest Chrome browser.










share|improve this question





























    1















    I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.



    And I'm using selenium 3.14.0 and latest Chrome browser.










    share|improve this question



























      1












      1








      1








      I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.



      And I'm using selenium 3.14.0 and latest Chrome browser.










      share|improve this question
















      I'm trying to get browser network logs using selenium to debug request/responses. Could you please help me to find out a way.



      And I'm using selenium 3.14.0 and latest Chrome browser.







      python selenium selenium-webdriver selenium-chromedriver






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 19:48









      Navarasu

      1,9731822




      1,9731822










      asked Nov 13 '18 at 17:49









      Anand SAnand S

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Using Python and ChromeDriver



          To get network logs, you need to install BrowserMobProxy as well along with selenium in python



          pip install browsermob-proxy


          You need to start browser proxy and configure the proxy in chrome option of chrome driver,



          from browsermobproxy import Server
          from selenium import webdriver

          server = Server("path/to/browsermob-proxy")
          server.start()
          proxy = server.create_proxy()

          # Configure the browser proxy in chrome options
          chrome_options = webdriver.ChromeOptions()
          chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
          browser = webdriver.Chrome(chrome_options = chrome_options)

          #tag the har(network logs) with a name
          proxy.new_har("google")


          Then you can navigate to page using selenium



          driver.get("http://www.google.co.in")


          After navigation, you can get the network logs in json format from the proxy



          print(proxy.har) # returns a Network logs (HAR) as JSON 


          Also before quitting the driver, stop the proxy server as well at the end,



          server.stop()
          browser.quit()





          share|improve this answer

































            0














            To get only the network logs up until the page has finished loading (no ajax/async network logs during the main usage of the page), you can get the Performance Log: http://chromedriver.chromium.org/logging/performance-log



            To enable the Performance Logging for the ChromeDriver, for example,



            DesiredCapabilities cap = DesiredCapabilities.chrome();
            LoggingPreferences logPrefs = new LoggingPreferences();
            logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
            cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
            RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);


            The chromium performance-log page also links to this complete example https://gist.github.com/klepikov/5457750 which has Java and python code to get the Performance Logs.



            Again, it's important to keep in mind that this will only get the network requests up until the point that the page is finished loading. After that, the driver will only return the same performance logs until the page reloads.





            If you want to get network logs asynchronously throughout the usage of the page, you can use BrowserMobProxy to act as a proxy server for your Selenium driver and capture all those network requests. Then, you can get those captured requests from BrowserMobProxy's generated HAR file: https://github.com/lightbody/browsermob-proxy#using-with-selenium



            // start the proxy
            BrowserMobProxy proxy = new BrowserMobProxyServer();
            proxy.start(0);

            // get the Selenium proxy object
            Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

            // configure it as a desired capability
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

            // start the browser up
            WebDriver driver = new FirefoxDriver(capabilities);

            // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
            proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

            // create a new HAR with the label "yahoo.com"
            proxy.newHar("yahoo.com");

            // open yahoo.com
            driver.get("http://yahoo.com");

            // get the HAR data
            Har har = proxy.getHar();


            Once you have the HAR file, it is a JSON like list of network events that you can work with.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286828%2fhow-to-get-browser-network-logs-using-python-selenium%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Using Python and ChromeDriver



              To get network logs, you need to install BrowserMobProxy as well along with selenium in python



              pip install browsermob-proxy


              You need to start browser proxy and configure the proxy in chrome option of chrome driver,



              from browsermobproxy import Server
              from selenium import webdriver

              server = Server("path/to/browsermob-proxy")
              server.start()
              proxy = server.create_proxy()

              # Configure the browser proxy in chrome options
              chrome_options = webdriver.ChromeOptions()
              chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
              browser = webdriver.Chrome(chrome_options = chrome_options)

              #tag the har(network logs) with a name
              proxy.new_har("google")


              Then you can navigate to page using selenium



              driver.get("http://www.google.co.in")


              After navigation, you can get the network logs in json format from the proxy



              print(proxy.har) # returns a Network logs (HAR) as JSON 


              Also before quitting the driver, stop the proxy server as well at the end,



              server.stop()
              browser.quit()





              share|improve this answer






























                2














                Using Python and ChromeDriver



                To get network logs, you need to install BrowserMobProxy as well along with selenium in python



                pip install browsermob-proxy


                You need to start browser proxy and configure the proxy in chrome option of chrome driver,



                from browsermobproxy import Server
                from selenium import webdriver

                server = Server("path/to/browsermob-proxy")
                server.start()
                proxy = server.create_proxy()

                # Configure the browser proxy in chrome options
                chrome_options = webdriver.ChromeOptions()
                chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
                browser = webdriver.Chrome(chrome_options = chrome_options)

                #tag the har(network logs) with a name
                proxy.new_har("google")


                Then you can navigate to page using selenium



                driver.get("http://www.google.co.in")


                After navigation, you can get the network logs in json format from the proxy



                print(proxy.har) # returns a Network logs (HAR) as JSON 


                Also before quitting the driver, stop the proxy server as well at the end,



                server.stop()
                browser.quit()





                share|improve this answer




























                  2












                  2








                  2







                  Using Python and ChromeDriver



                  To get network logs, you need to install BrowserMobProxy as well along with selenium in python



                  pip install browsermob-proxy


                  You need to start browser proxy and configure the proxy in chrome option of chrome driver,



                  from browsermobproxy import Server
                  from selenium import webdriver

                  server = Server("path/to/browsermob-proxy")
                  server.start()
                  proxy = server.create_proxy()

                  # Configure the browser proxy in chrome options
                  chrome_options = webdriver.ChromeOptions()
                  chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
                  browser = webdriver.Chrome(chrome_options = chrome_options)

                  #tag the har(network logs) with a name
                  proxy.new_har("google")


                  Then you can navigate to page using selenium



                  driver.get("http://www.google.co.in")


                  After navigation, you can get the network logs in json format from the proxy



                  print(proxy.har) # returns a Network logs (HAR) as JSON 


                  Also before quitting the driver, stop the proxy server as well at the end,



                  server.stop()
                  browser.quit()





                  share|improve this answer















                  Using Python and ChromeDriver



                  To get network logs, you need to install BrowserMobProxy as well along with selenium in python



                  pip install browsermob-proxy


                  You need to start browser proxy and configure the proxy in chrome option of chrome driver,



                  from browsermobproxy import Server
                  from selenium import webdriver

                  server = Server("path/to/browsermob-proxy")
                  server.start()
                  proxy = server.create_proxy()

                  # Configure the browser proxy in chrome options
                  chrome_options = webdriver.ChromeOptions()
                  chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
                  browser = webdriver.Chrome(chrome_options = chrome_options)

                  #tag the har(network logs) with a name
                  proxy.new_har("google")


                  Then you can navigate to page using selenium



                  driver.get("http://www.google.co.in")


                  After navigation, you can get the network logs in json format from the proxy



                  print(proxy.har) # returns a Network logs (HAR) as JSON 


                  Also before quitting the driver, stop the proxy server as well at the end,



                  server.stop()
                  browser.quit()






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '18 at 18:43

























                  answered Nov 13 '18 at 18:38









                  NavarasuNavarasu

                  1,9731822




                  1,9731822

























                      0














                      To get only the network logs up until the page has finished loading (no ajax/async network logs during the main usage of the page), you can get the Performance Log: http://chromedriver.chromium.org/logging/performance-log



                      To enable the Performance Logging for the ChromeDriver, for example,



                      DesiredCapabilities cap = DesiredCapabilities.chrome();
                      LoggingPreferences logPrefs = new LoggingPreferences();
                      logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
                      cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                      RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);


                      The chromium performance-log page also links to this complete example https://gist.github.com/klepikov/5457750 which has Java and python code to get the Performance Logs.



                      Again, it's important to keep in mind that this will only get the network requests up until the point that the page is finished loading. After that, the driver will only return the same performance logs until the page reloads.





                      If you want to get network logs asynchronously throughout the usage of the page, you can use BrowserMobProxy to act as a proxy server for your Selenium driver and capture all those network requests. Then, you can get those captured requests from BrowserMobProxy's generated HAR file: https://github.com/lightbody/browsermob-proxy#using-with-selenium



                      // start the proxy
                      BrowserMobProxy proxy = new BrowserMobProxyServer();
                      proxy.start(0);

                      // get the Selenium proxy object
                      Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

                      // configure it as a desired capability
                      DesiredCapabilities capabilities = new DesiredCapabilities();
                      capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

                      // start the browser up
                      WebDriver driver = new FirefoxDriver(capabilities);

                      // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
                      proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

                      // create a new HAR with the label "yahoo.com"
                      proxy.newHar("yahoo.com");

                      // open yahoo.com
                      driver.get("http://yahoo.com");

                      // get the HAR data
                      Har har = proxy.getHar();


                      Once you have the HAR file, it is a JSON like list of network events that you can work with.






                      share|improve this answer




























                        0














                        To get only the network logs up until the page has finished loading (no ajax/async network logs during the main usage of the page), you can get the Performance Log: http://chromedriver.chromium.org/logging/performance-log



                        To enable the Performance Logging for the ChromeDriver, for example,



                        DesiredCapabilities cap = DesiredCapabilities.chrome();
                        LoggingPreferences logPrefs = new LoggingPreferences();
                        logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
                        cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                        RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);


                        The chromium performance-log page also links to this complete example https://gist.github.com/klepikov/5457750 which has Java and python code to get the Performance Logs.



                        Again, it's important to keep in mind that this will only get the network requests up until the point that the page is finished loading. After that, the driver will only return the same performance logs until the page reloads.





                        If you want to get network logs asynchronously throughout the usage of the page, you can use BrowserMobProxy to act as a proxy server for your Selenium driver and capture all those network requests. Then, you can get those captured requests from BrowserMobProxy's generated HAR file: https://github.com/lightbody/browsermob-proxy#using-with-selenium



                        // start the proxy
                        BrowserMobProxy proxy = new BrowserMobProxyServer();
                        proxy.start(0);

                        // get the Selenium proxy object
                        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

                        // configure it as a desired capability
                        DesiredCapabilities capabilities = new DesiredCapabilities();
                        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

                        // start the browser up
                        WebDriver driver = new FirefoxDriver(capabilities);

                        // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
                        proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

                        // create a new HAR with the label "yahoo.com"
                        proxy.newHar("yahoo.com");

                        // open yahoo.com
                        driver.get("http://yahoo.com");

                        // get the HAR data
                        Har har = proxy.getHar();


                        Once you have the HAR file, it is a JSON like list of network events that you can work with.






                        share|improve this answer


























                          0












                          0








                          0







                          To get only the network logs up until the page has finished loading (no ajax/async network logs during the main usage of the page), you can get the Performance Log: http://chromedriver.chromium.org/logging/performance-log



                          To enable the Performance Logging for the ChromeDriver, for example,



                          DesiredCapabilities cap = DesiredCapabilities.chrome();
                          LoggingPreferences logPrefs = new LoggingPreferences();
                          logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
                          cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                          RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);


                          The chromium performance-log page also links to this complete example https://gist.github.com/klepikov/5457750 which has Java and python code to get the Performance Logs.



                          Again, it's important to keep in mind that this will only get the network requests up until the point that the page is finished loading. After that, the driver will only return the same performance logs until the page reloads.





                          If you want to get network logs asynchronously throughout the usage of the page, you can use BrowserMobProxy to act as a proxy server for your Selenium driver and capture all those network requests. Then, you can get those captured requests from BrowserMobProxy's generated HAR file: https://github.com/lightbody/browsermob-proxy#using-with-selenium



                          // start the proxy
                          BrowserMobProxy proxy = new BrowserMobProxyServer();
                          proxy.start(0);

                          // get the Selenium proxy object
                          Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

                          // configure it as a desired capability
                          DesiredCapabilities capabilities = new DesiredCapabilities();
                          capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

                          // start the browser up
                          WebDriver driver = new FirefoxDriver(capabilities);

                          // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
                          proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

                          // create a new HAR with the label "yahoo.com"
                          proxy.newHar("yahoo.com");

                          // open yahoo.com
                          driver.get("http://yahoo.com");

                          // get the HAR data
                          Har har = proxy.getHar();


                          Once you have the HAR file, it is a JSON like list of network events that you can work with.






                          share|improve this answer













                          To get only the network logs up until the page has finished loading (no ajax/async network logs during the main usage of the page), you can get the Performance Log: http://chromedriver.chromium.org/logging/performance-log



                          To enable the Performance Logging for the ChromeDriver, for example,



                          DesiredCapabilities cap = DesiredCapabilities.chrome();
                          LoggingPreferences logPrefs = new LoggingPreferences();
                          logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
                          cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                          RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);


                          The chromium performance-log page also links to this complete example https://gist.github.com/klepikov/5457750 which has Java and python code to get the Performance Logs.



                          Again, it's important to keep in mind that this will only get the network requests up until the point that the page is finished loading. After that, the driver will only return the same performance logs until the page reloads.





                          If you want to get network logs asynchronously throughout the usage of the page, you can use BrowserMobProxy to act as a proxy server for your Selenium driver and capture all those network requests. Then, you can get those captured requests from BrowserMobProxy's generated HAR file: https://github.com/lightbody/browsermob-proxy#using-with-selenium



                          // start the proxy
                          BrowserMobProxy proxy = new BrowserMobProxyServer();
                          proxy.start(0);

                          // get the Selenium proxy object
                          Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);

                          // configure it as a desired capability
                          DesiredCapabilities capabilities = new DesiredCapabilities();
                          capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);

                          // start the browser up
                          WebDriver driver = new FirefoxDriver(capabilities);

                          // enable more detailed HAR capture, if desired (see CaptureType for the complete list)
                          proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);

                          // create a new HAR with the label "yahoo.com"
                          proxy.newHar("yahoo.com");

                          // open yahoo.com
                          driver.get("http://yahoo.com");

                          // get the HAR data
                          Har har = proxy.getHar();


                          Once you have the HAR file, it is a JSON like list of network events that you can work with.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 18:05









                          George PantazesGeorge Pantazes

                          472617




                          472617






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286828%2fhow-to-get-browser-network-logs-using-python-selenium%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              The Sandy Post

                              Danny Elfman

                              Pages that link to "Head v. Amoskeag Manufacturing Co."