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.

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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) 

2 Comments

is there any special reason for using JavaScript?
Because my present project is using JavaScript. I don't want to change my code to JavaScript. BTW, I am not a Python Guy.
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.

1 Comment

For anyone coming across after July 2017, I found it worked in Chrome 61 with default_content_setting_values.
0
 import { Options } from 'selenium-webdriver/chrome'; const options = new Options(); options.setUserPreferences({ 'profile.managed_default_content_settings.images': 2, //disable loading }); 

1 Comment

A code-only answer is not high quality. 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.