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.
browser. close() will close only the current chrome window. browser. quit() should close all of the open windows, then exit webdriver.
--disable-gpu doesn't run the script without opening the browser, only --headless .
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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With