Enable "preserve log" with chromedriver
Asked Answered
E

3

12

Looking to enable "Preserve log" with chromedriver. https://sites.google.com/a/chromium.org/chromedriver/capabilities loggingPrefs points to google code archive page and is not very helpful.

My overall goal is to parse logs during test execution looking for errors, but logs get cleared on page navigation which may happen multiple times during test.

I can think of couple non-ideal workarounds:

  • parse log on every page navigation
  • log to file and parse log later

Both are not ideal, so looking for the easiest way.

This question is similar to Enable "Preserve log" in chrome programmatically using chromedriver - but over there the answer seems to be around logging redirects via performance logging, not preserving log on navigation.

Erlin answered 30/11, 2017 at 17:52 Comment(3)
if you log to file, can you not read that while your code is executing? Can you try something like this #4401017Romish
@CavanPage yeah - I could do that, that's the second workaround I listed. Looking if there is an easier option.Katzman
Well you said parse it later, but I am suggesting you can read it while it is still getting written to. You said you want to parse during execution.Romish
E
1

To enable the "Preserve log" feature with chromedriver, you can use the loggingPrefs capability in your chromedriver options. This capability allows you to specify which types of log messages to capture, and where to send them.

I'll give an example of how to enable the "Preserve log" feature in Python: from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--enable-logging')
options.add_argument('--v=1')
capabilities = {'loggingPrefs': {'browser': 'ALL'}}

driver = webdriver.Chrome(options=options, desired_capabilities=capabilities)

In this example, we're creating a new ChromeOptions object, and adding two arguments: --enable-logging and --v=1. These arguments enable verbose logging in the Chrome browser, which is necessary to capture all log messages.

We're also creating a loggingPrefs capability object, with the browser key set to 'ALL'. This tells chromedriver to capture all log messages, including network requests and JavaScript console messages.

Finally, we're passing both the options and capabilities objects to the webdriver.Chrome() constructor, which creates a new ChromeDriver instance with the desired options.

Note that even with the "Preserve log" feature enabled, the log messages will still be cleared when the page is navigated. However, you can use the driver.get_log('browser') method to retrieve all captured log messages at any time during your test.

Eatton answered 24/2, 2023 at 9:28 Comment(0)
G
1

Selenium from v4.0 have a service parameter, just set log_path, It will append if the file already exists.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_argument('--enable-logging')
options.add_argument('--v=1')
s = ChromeService(log_path="./chromedriver.log")
driver = webdriver.Chrome(service=s, options=options)

The above desired_capabilities was removed in selenium v4.10 for python. But it did not preserve logs, it overwrite on each initialization.

Gentoo answered 19/6, 2023 at 7:54 Comment(0)
R
-1
//chromedriver,preserve log
// request postdata
ChromeOptions chromeOptions = new ChromeOptions();
//HAR
ArrayList<String> excludeSwitches = new ArrayList<>();
excludeSwitches.add("enable-automation");
chromeOptions.setExperimentalOption("excludeSwitches",excludeSwitches);
List<HarEntry> list =  har.getLog().getEntries();
for (HarEntry harEntry : list){
  HarRequest request = harEntry.getRequest();
  if(request != null){
    String requestBody = request.getPostData().getText();
    System.out.println("requestBody:" + requestBody);
  }
}
Russo answered 18/10, 2022 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.