20

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!

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Does it work? I think, it needs False instead of string "false". It should be boolean. Pls advice.
@mrtipale, yes, it needs to be boolean.
It depends to language that you are use. In some languages it is bool, in some - string.
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.