jpjacobpadilla / Google-Colab-Selenium

The best way to use Selenium in Google Colab Notebooks!
https://pypi.org/project/google-colab-selenium/
MIT License
156 stars 18 forks source link

unable to add_experimental_option #8

Closed mzz92 closed 4 months ago

mzz92 commented 4 months ago

This is a fantastic package. I would like to disable automatic image downloads using add_experimental_option:

prefs = {
  "profile.managed_default_content_settings.images": 2,  
  "permissions.default.stylesheet": 2,  
}

options.add_experimental_option("prefs", prefs)

However, I get this error:

InvalidArgumentException: Message: invalid argument: cannot parse capability: goog:chromeOptions from invalid argument: unrecognized chrome option: prefs

Is there some other way I should be doing this? Thanks!

jpjacobpadilla commented 4 months ago

I'm glad you find the package useful!

Try this:

To add these options to the regular ChromeDriver, do the following:

import google_colab_selenium as gs
from selenium.webdriver.chrome.options import Options

options = Options()

prefs = {
  "profile.managed_default_content_settings.images": 2,  
  "permissions.default.stylesheet": 2,  
}

options.add_experimental_option("prefs", prefs)

driver = gs.Chrome(options=options)

And if you're using the Undetected-ChromeDriver package, you can use that package's custom options like so:

import google_colab_selenium as gs
import undetected_chromedriver as uc

options = uc.ChromeOptions()

prefs = {
  "profile.managed_default_content_settings.images": 2,  
  "permissions.default.stylesheet": 2,  
}

options.add_experimental_option("prefs", prefs)

driver = gs.UndetectedChrome(options=options)