Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Disable images in Selenium Google ChromeDriver

I spend a lot of time searching about this. At the end of the day I combined a number of answers and it works. I share my answer and I'll appreciate it if anyone edits it or provides us with an easier way to do this.

1- The answer in Disable images in Selenium Google ChromeDriver works in Java. So we should do the same thing in Python:

opt = webdriver.ChromeOptions() opt.add_extension("Block-image_v1.1.crx") browser = webdriver.Chrome(chrome_options=opt) 

2- But downloading "Block-image_v1.1.crx" is a little bit tricky, because there is no direct way to do that. For this purpose, instead of going to: https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj

you can go to http://chrome-extension-downloader.com/ and paste the extension url there to be able to download the extension file.

3- Then you will be able to use the above mentioned code with the path to the extension file that you have downloaded.

like image 296
1man Avatar asked Jan 21 '15 15:01

1man


People also ask

How do I disable ChromeDriver?

browser. close() will close only the current chrome window. browser. quit() should close all of the open windows, then exit webdriver.

What is disable GPU in Selenium?

--disable-gpu doesn't run the script without opening the browser, only --headless .

Can a website detect when you are using Selenium with ChromeDriver Python?

In theory, chromedriver and Chrome should look literally exactly the same to any webserver, but somehow they can detect it. If you browse around stubhub you'll get redirected and 'blocked' within one or two requests.


Video Answer


2 Answers

Here is another way to disable images:

from selenium import webdriver  chrome_options = webdriver.ChromeOptions() prefs = {"profile.managed_default_content_settings.images": 2} chrome_options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(chrome_options=chrome_options) 

I found it below:

http://nullege.com/codes/show/src@o@s@[email protected]/56/selenium.webdriver.ChromeOptions.add_experimental_option

like image 92
rocky qi Avatar answered Sep 17 '22 09:09

rocky qi


Java: With this Chrome nor Firefox would load images. The syntax is different but the strings on the parameters are the same.

    chromeOptions = new ChromeOptions();     HashMap<String, Object> images = new HashMap<String, Object>();     images.put("images", 2);     HashMap<String, Object> prefs = new HashMap<String, Object>();     prefs.put("profile.default_content_setting_values", images);     chromeOptions.setExperimentalOption("prefs", prefs);     driver=new ChromeDriver(chromeOptions);      firefoxOpt = new FirefoxOptions();     FirefoxProfile profile = new FirefoxProfile();     profile.setPreference("permissions.default.image", 2);     firefoxOpt.setProfile(profile); 
like image 26
user3453444 Avatar answered Sep 21 '22 09:09

user3453444