Python Selenium Chromedriver 无法禁止图像加载

Python Selenium Chromedriver Can't disable images to load

我想禁止使用 Selenium 在 Chrome 中加载图像,

当我使用此代码(以及我在网上找到的其他代码)时:

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)

我收到这些错误消息:

<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

编辑 1

最后我按照建议尝试的是:

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)

但是这一行:

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

导致此错误,同时我的 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:\Users791\.wdm\drivers\chromedriver\win32.0.4692.71\chromedriver.exe] found in cache

和这一行:

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

我做到了:

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()