Alex M.
Alex M.

Reputation: 361

Disable CSS and image in Selenium Chromedriver, Python

I want to disable CSS and images in Selenium with Chromedriver all done in Python. My current code looks like this:

from selenium import webdriver
chrome_path = r"/Folder/chromedriver"
driver = webdriver.Chrome(chrome_path)

driver.get("https://www.url.com")

All works and it loads the page, but I want to speed it up and only load the Dom tree and javascripts, because I need this to click a button.

Someone else asked this question and got this answer:

chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)

However this doesn't work because I guess it doesn't find the driver. As far as the images go I really don't have an idea to stop loading them.

Upvotes: 4

Views: 3783

Answers (1)

mgc
mgc

Reputation: 5443

I guess you just forgot to specify the chrome path (in your chrome_path variable) in the second snippet (and in the first snippet your aren't using your chrome options but your are specifying the chrome path).
You should try to specify them both when creating the webdriver.Chrome instance with something like :

from selenium import webdriver

chrome_path = r"/Folder/chromedriver"

chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chromeOptions.add_experimental_option("prefs",prefs)

driver = webdriver.Chrome(chrome_path, chrome_options=chromeOptions)
driver.get("https://www.url.com")

Upvotes: 2

Related Questions