0

I'm trying to create a driver in Selenium 4.10 that will collect all of the performance logs.

The below is what've tried and it doesn't work. From what I've been gathering we need to use the options class to set the logging level as it has replaced the DesiredCapabilities Class. webdriver.Chrome doesn't have an attribute of desired_capabilties.

def get_driver(self):
    opts = webdriver.ChromeOptions()
    opts.add_argument("--disable-blink-features=AutomationControlled")
    opts.add_argument("--ignore-certificate-errors")
    opts.add_argument(
        f"--user-agent=Lynx: Lynx/2.8.8pre.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.12.23"
    )
    opts.page_load_strategy = "normal"

    service = Service(
        service_args=["--log-level=ALL", "--append-log"],
        log_path="LOGCHROME.txt",
    )

    driver = webdriver.Chrome(service=service, options=opts)
    driver.execute_cdp_cmd("Performance.enable", {})
    
    return driver

Here is what works with early versions of selenium and what I am trying to recreate:

def get_driver(self):
    desired_capabilities = DesiredCapabilities.CHROME
    desired_capabilities["goog:loggingPrefs"] = {"performance": "ALL"}
    options = webdriver.ChromeOptions()
    options.add_argument("headless")
    options.add_argument("--ignore-certificate-errors")
    driver = webdriver.Chrome(
            service=Service(ChromeDriverManager().install()),
            options=options,
            desired_capabilities=desired_capabilities,
        )
    return driver

The reason for me upgrading to a newer version of Selenium, is webdriver_manager chrome has run into a bug where it thinks the driver should be win32. Selenium 4.10 doesn't require the webdriver_manger hence the upgrade.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Evan
  • 1
  • 2

2 Answers2

0

DesiredCapabilities which was deprecated, is now removed in Selenium v4.10.


Solution

You have to use an instance of ChromeOptions set the capability goog:loggingPrefs as {'performance': 'ALL'}. The minimum effective code block will be:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
driver = Chrome(options=options)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

This is why you don't code late at night. The options class now comes with a method to set capabilities. Below is the code that fixed the above issue:

   def get_driver(self):
        opts = webdriver.ChromeOptions()
        opts.add_argument("--disable-blink-features=AutomationControlled")
        opts.add_argument("--ignore-certificate-errors")
        opts.add_argument(
            f"--user-agent=Lynx: Lynx/2.8.8pre.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.12.23"
        )
        opts.add_argument("headless")
        opts.page_load_strategy = "normal"
        opts.set_capability("goog:loggingPrefs", {"performance": "ALL"})
        driver = webdriver.Chrome(service=Service(), options=opts)
        return driver```

Evan
  • 1
  • 2