Disable images in Selenium Python

0
0

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 :/

  • You must to post comments
0
0

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

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website