0

Because Webdriver waits for the entire page to load before going on to the next line, I want to disable images will speed things up when the network is slow.

This is the example js file in Selenium Webdriver's website:

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();

How can I disable image in my code?

I have search google for this question, I only get this solution in Python: Disable images in Selenium Python.

Community
  • 1
  • 1
Feng Yu
  • 359
  • 1
  • 4
  • 17

4 Answers4

1

On a high level, I see some solutions:

  • set profile.managed_default_content_settings.images to 2 (I can't find the corresponding chromedriver documentation, but you can google it).
  • Set up a proxy. Connect to your page via a proxy that returns empty data when asking for an image file.
  • load the browser with a browser plugin that does this for you. Something (a bit like ad-blocked works) might be available already. (con: browser-specific solution)
Len
  • 2,093
  • 4
  • 34
  • 51
0

Here I give you code for not loading image.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option( "prefs", {'profile.managed_default_content_settings.images': 2})
driver = webdriver.Chrome("chromedriver.exe",chrome_options=chrome_options)
Piyush
  • 511
  • 4
  • 13
0

You can pass an optionsobject to WebdriverJS' Builder that disables images:

{
    prefs: {
        profile: {
            managed_default_content_settings: {
                images: 2
            }
        }
    }
}

The complete example is:

const chromeDesktop = {
    prefs: {
        profile: {
            managed_default_content_settings: {
                images: 2
            }
        }
    }
};
const { By, Builder, until } = require('selenium-webdriver');
const driver = new Builder().withCapabilities(chromeDesktop).build();

This definitely worked for me.

JamieJag
  • 1,541
  • 2
  • 11
  • 18
  • For anyone coming across after July 2017, I found it worked in Chrome 61 with `default_content_setting_values`. – Sean Jul 19 '17 at 04:58
-1
      import { Options } from 'selenium-webdriver/chrome';
     
      const options = new Options();
    
    
      options.setUserPreferences({
    
        'profile.managed_default_content_settings.images': 2, //disable loading 
      });
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Stephen Ostermiller Jan 12 '23 at 15:31