Python Selenium how to disable images in Firefox

Question:

I am trying to disable images in Selenium (Python) using Firefox as webdriver and this code works:

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/')

but apparently firefox_profile has been deprecated because I get always the same message:

Warning (from warnings module):
  File "C:UsersuserDesktoptest.py", line 3
    firefox_profile = webdriver.FirefoxProfile()
DeprecationWarning: firefox_profile has been deprecated, please use an Options object

Warning (from warnings module):
  File "C:UsersuserDesktoptest.py", line 8
    driver = webdriver.Firefox(firefox_profile=firefox_profile)
DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object 

Does anyone know how to do the same with the code updated??

Asked By: Claytor

||

Answers:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference('permissions.default.stylesheet', 2)
options.set_preference('permissions.default.image', 2)
options.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')

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

In this updated code, we create an instance of the Options object and use the set_preference() method to set the desired preferences to disable images, stylesheets, and Flash. We then pass the options object to the Firefox() constructor to create a new instance of the Firefox webdriver with the desired preferences

Answered By: Hirok Sarker