Disable images in Selenium Python

14,091

Solution 1

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:

enter image description here

Solution 2

Unfortunately the option firefox_profile.set_preference('permissions.default.image', 2) will no longer work to disable images with the latest version of Firefox - [for reason see Alecxe's answer to my question Can't turn off images in Selenium / Firefox ]

The best solution i had was to use the firefox extension quickjava , which amongst other things can disable images- https://addons.mozilla.org/en-us/firefox/addon/quickjava/

My Python code:

 from selenium import webdriver
 firefox_profile = webdriver.FirefoxProfile()

 firefox_profile.add_extension(folder_xpi_file_saved_in + "\\quickjava-2.0.6-fx.xpi")
 firefox_profile.set_preference("thatoneguydotnet.QuickJava.curVersion", "2.0.6.1") ## Prevents loading the 'thank you for installing screen'
 firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Images", 2)  ## Turns images off
 firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.AnimatedImage", 2)  ## Turns animated images off

 driver = webdriver.Firefox(firefox_profile)
 driver.get(web_address_desired)

Other things can also be switched off by adding the lines:

  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.CSS", 2)  ## CSS
  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Cookies", 2)  ## Cookies
  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Flash", 2)  ## Flash
  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Java", 2)  ## Java
  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.JavaScript", 2)  ## JavaScript
  firefox_profile.set_preference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2)  ## Silverlight

Solution 3

The accepted answer doesn't work for me either. From the "reason" links referred by kyrenia I gathered that Firefox overrides the "permissions.default.image" preference on the first startup and I was able to prevent that by doing:

# Arbitrarily high number
profile.set_preference('browser.migration.version', 9001)

Which seems to be ok since I create the profile on each driver startup so there is nothing to actually be migrated.

Solution 4

I understand this is a question, but it helped me with facebook/php-webdriver. (First result in search engine for php webdriver disable javascript)

I thought I'd post my code (altered version of @kyrenia answer for ) to help others get up and running.


Install Everything

  1. Download and install facebook/php-webdriver. composer require facebook/webdriver

  2. Download Selenium & Start it. java -jar selenium-server-standalone-#.jar

  3. Download Quick Java and place it into your project directory.


Usage

use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

// Change this to the path of you xpi
$extensionPath = $this->container->getParameter('kernel.root_dir').'/../bin/selenium/quickjava-2.0.6-fx.xpi';

// Build our firefox profile
$profile = new FirefoxProfile();
$profile->addExtension($extensionPath);
$profile->setPreference('thatoneguydotnet.QuickJava.curVersion', '2.0.6.1');
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Images', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.CSS', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Cookies', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Flash', 2);
$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.Java', 2);
//$profile->setPreference('thatoneguydotnet.QuickJava.startupStatus.JavaScript', 2);
$profile->setPreference("thatoneguydotnet.QuickJava.startupStatus.Silverlight", 2);

// Create DC
$dc = DesiredCapabilities::firefox();
$dc->setCapability(FirefoxDriver::PROFILE, $profile);

// Create our new driver
$driver = RemoteWebDriver::create($host, $dc);
$driver->get('http://stackoverflow.com');

// The HTML Source code
$html = $driver->getPageSource();

// Firefox should be open and you can see no images or css was loaded

View more preference settings here: https://github.com/ThatOneGuyDotNet/QuickJava/blob/master/defaults/preferences/defaults.js

Share:
14,091
Jack
Author by

Jack

Updated on June 04, 2022

Comments

  • Jack
    Jack almost 2 years

    Because Webdriver waits for the entire page to load before going on to the next line, I think disabling images, css and javascript will speed things up.

    from selenium import webdriver
    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)
    

    I got the code from stackoverflow Do not want images to load and CSS to render on Firefox in Selenium WebDriver tests with Python

    But when I add

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

    to the end, it still loads images :/

  • kyrenia
    kyrenia almost 9 years
    This answer unfortunately will no longer work, because firefox won't allow changes to the default.image value any more - thanks to alecxe who pointed me in the right direction - my code to solve is below.
  • m3nda
    m3nda over 8 years
    @kyrenia really? I have Firefox 41 and still does work.
  • m3nda
    m3nda over 8 years
    Interesting Java extension :D never see it.
  • kyrenia
    kyrenia over 8 years
    @erm3nda - was certainly a problem I was experiencing around July [2015], which would have probably been Firefox 39 - if no longer applies, then disabling via firefox_profile.set_preference('permissions.default.image', 2) is obviously a cleaner solution.
  • m3nda
    m3nda over 8 years
    Because it's obvious, i commented it... some lazy reader can think that using a extension it's better without even try it ;P
  • Ivan Bilan
    Ivan Bilan over 8 years
    This should be best answer! Thanks, this helped me a lot.
  • Nuno André
    Nuno André about 8 years
    I had to use 2.0.6 version. Last version (2.0.7) raises an AddonFormatError exception. It seems having something to do with the manifest file but both are the same.
  • Frank
    Frank about 5 years
    Setting permissions.default.image works for me on Firefox 66.0.5.
  • Brad Ahrens
    Brad Ahrens over 4 years
    I added Image Block X, which works great :) addons.mozilla.org/en-US/firefox/addon/image-block-x