Do Not Want the Images to Load and CSS to Render on Firefox in Selenium Webdriver - Python

Do not want the Images to load and CSS to render on Firefox in Selenium WebDriver - Python

I have figured out a way to prevent Firefox from loading CSS, images and Flash.

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

def disableImages(self):
## get the Firefox profile object
firefoxProfile = FirefoxProfile()
## Disable CSS
firefoxProfile.set_preference('permissions.default.stylesheet', 2)
## Disable images
firefoxProfile.set_preference('permissions.default.image', 2)
## Disable Flash
firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so',
'false')
## Set the modified profile while creating the browser object
self.browserHandle = webdriver.Firefox(firefoxProfile)

Thanks again @Simon and @ernie for your suggestions.

Disable images in Selenium Python

UPDATE: The answer might not work any longer since permissions.default.image became a frozen setting and cannot be changed. Please try with quickjava extension (link to the answer).


You need to pass firefox_profile instance to the webdriver constructor:

from selenium import webdriver

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get('http://www.stackoverflow.com/')

driver.close()

And this is how it would be displayed:

Sample Image

Can't turn off images in Selenium / Firefox

From what I understand, this problem is related to the following Firefox issues:

  • Remove "Load images automatically" checkbox from Prefs
  • Get rid of options that kill our product

That means that permissions.default.image is frozen, cannot be changed and does nothing.


Alternatives:

  • use Image Block extension
  • switch to Chrome (Disable images in Selenium ChromeDriver)

How to prevent css and img download in selenium

If you check phantomjs API, an option can be passed as:
--load-images=[true|false] load all inlined images (default is true). Also accepted: [yes|no].

However, you cannot block CSS download AFAIK.



Related Topics



Leave a reply



Submit