Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 835
ohad edelstain Avatar asked Jul 05 '15 19:07

ohad edelstain


People also ask

How do I disable images in Firefox?

Chosen SolutionUnder the Permissions tab, scroll down to 'Load images' - uncheck 'Use Default' and check 'Block'. You can disable images on the '''about:config''' page : Type '''about:config''' in the address bar and press Enter (accept the risk, if asked) Type in the search bar : '''permissions.

How do I stop Python from Selenium from loading?

New Selenium IDE To stop a page loading, the command window. stop() is passed as a parameter to the executeScript method. Also, for the Chrome browser we have to configure the pageLoadStrategy to none value and wait for the web element to be available.

How do I use Firefox Webdriver in Python?

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript. Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.


2 Answers

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.

like image 189
Andrew Avatar answered Oct 15 '22 01:10

Andrew


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

like image 21
Salek Avatar answered Oct 14 '22 23:10

Salek