ohad edelstain
ohad edelstain

Reputation: 1645

Selenium: How to disable image loading with firefox and python?

I have read similar questions and one was supposed to be the answer, but when I tried it, it only gave a partial solution. I refer to this question: Disable images in Selenium Python

My problem is that I tried the solution and some of the images do not appear, but images that arrive from:

<img href="www.xxx.png"> 

Are being loaded. Is there a way to tell firefox/selenium not to get it? If not, is there a way to discard it from the dom element that I get back, via:

self._browser.get(url)
content=self._browser.page_source

for example by doing some kind of find replace on the dom tree? The browser configuration is the same browser from the previous question:

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

**Update**:

I kept on digging and what I learned is that if I inspect the text document that the selenium/firefox combo did I see that, it didn't bring the images and kept them as links. But when I did:

self._browser.save_screenshot("info.png") 

I got a 24 mega file with all the img links loaded.
Can anyone explain to me this matter?
Thanks!

Upvotes: 20

Views: 25113

Answers (2)

Salek
Salek

Reputation: 451

In the latest Firefox versions permissions.default.image can't be changed. To disable the images, either switch to ChromeDriver or use alternative extentions as suggested here.

Upvotes: 5

Andrew_STOP_RU_WAR_IN_UA
Andrew_STOP_RU_WAR_IN_UA

Reputation: 11426

You can disable images using the following code:

firefox_profile = webdriver.FirefoxProfile()
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)

if you need to block some specific url... hm... I think you need to add string:

127.0.0.1 www.someSpecificUrl.com 

to the hosts file before test start and delete it after test finish.

Upvotes: 23

Related Questions