0

I would like to disable images to load in Chrome with Selenium,

when I use this code (and other codes I found online):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

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 get these error messages:

<ipython-input-36-fb16a130c9b1>:7: DeprecationWarning: use options instead of chrome_options   driver = webdriver.Chrome(chrome_options=chrome_options)


WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

EDIT 1

last I Tried as suggested was:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

    
driver =webdriver.Chrome(ChromeDriverManager().install(),options=options)
        
options = Options()

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

but this line :

driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

drives this error, meanwhile my chromedriver-py 97.0.4692.71:

====== WebDriver manager ======
Current google-chrome version is 97.0.4692
Get LATEST chromedriver version for 97.0.4692 google-chrome
Driver [C:\Users\48791\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe] found in cache

and this line:

driver=webdriver.Chrome(options=options)

WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Skittles
  • 121
  • 1
  • 11
  • Does this answer your question? [Error message: "'chromedriver' executable needs to be available in the path"](https://stackoverflow.com/questions/29858752/error-message-chromedriver-executable-needs-to-be-available-in-the-path) – Prophet Jan 06 '22 at 16:45
  • No, I hopped for some easier solution, tried this one but I get this : ====== WebDriver manager ====== Current google-chrome version is 97.0.4692 Get LATEST chromedriver version for 97.0.4692 google-chrome Driver [C:\Users\48791\.wdm\drivers\chromedriver\win32\97.0.4692.71\chromedriver.exe] found in cache :12: DeprecationWarning: use options instead of chrome_options driver = webdriver.Chrome(chrome_options=chrome_options) – Skittles Jan 06 '22 at 16:58
  • @Prophet my current ChromeDriver version is 97.0.4692.71 – Skittles Jan 06 '22 at 16:58
  • As the stack said, You'll have to use the chrome options and use the correct flag to do so. I don't think there is a flag to that on command line, but I'll look for it and edit this comment. – ojonasplima Jan 06 '22 at 17:03
  • I confirmed that you can't do it with the common flags, you should take a look at [this post](https://bugs.chromium.org/p/chromedriver/issues/detail?id=20) that may help you overwriting the chromeoptions. – ojonasplima Jan 06 '22 at 17:30
  • The explanation is pretty self explanatory use Options instead of ChromeOptions(). Use the following: from selenium.webdriver.chrome.options import Options options = Options() driver=webdriver.Chrome(options=options) – Arundeep Chohan Jan 06 '22 at 20:13
  • For the chromedriver path I would recommend using webdriver manager (pip install this)which you can just from webdriver_manager.chrome import ChromeDriverManager and then driver = webdriver.Chrome(ChromeDriverManager().install(),options=options). – Arundeep Chohan Jan 06 '22 at 20:18
  • @ArundeepChohan thank you for the answer, but still does not work, updated the question with your suggestion. – Skittles Jan 06 '22 at 20:46
  • Follow the second comment for that issue – Arundeep Chohan Jan 06 '22 at 21:43
  • also I think chrome version 97 doesn't have a matching driver so go down to 96 – Arundeep Chohan Jan 06 '22 at 21:44

1 Answers1

0

I managed to do it:

option = webdriver.ChromeOptions()
chrome_prefs = {}
option.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
PATH = 'C:\Program Files (x86)\chromedriver.exe'
browser = webdriver.Chrome(executable_path = PATH, options=option)
browser.get('https://www.yahoo.com/')
browser.find_element_by_xpath('//*[@class="btn primary"]').click()
Skittles
  • 121
  • 1
  • 11