Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:禁用Selenium Google ChromeDriver中的图像_Python_Google Chrome_Selenium_Web Scraping_Web Crawler - Fatal编程技术网

Python:禁用Selenium Google ChromeDriver中的图像

Python:禁用Selenium Google ChromeDriver中的图像,python,google-chrome,selenium,web-scraping,web-crawler,Python,Google Chrome,Selenium,Web Scraping,Web Crawler,我花了很多时间寻找这个。 在一天结束的时候,我把一些答案结合起来,结果很有效。我与大家分享我的答案,如果有人对其进行编辑或为我们提供一种更简单的方法,我将不胜感激 1-答案在Java中有效。因此,我们应该在Python中做同样的事情: opt = webdriver.ChromeOptions() opt.add_extension("Block-image_v1.1.crx") browser = webdriver.Chrome(chrome_options=opt) 2-但是下载“Blo

我花了很多时间寻找这个。 在一天结束的时候,我把一些答案结合起来,结果很有效。我与大家分享我的答案,如果有人对其进行编辑或为我们提供一种更简单的方法,我将不胜感激

1-答案在Java中有效。因此,我们应该在Python中做同样的事情:

opt = webdriver.ChromeOptions()
opt.add_extension("Block-image_v1.1.crx")
browser = webdriver.Chrome(chrome_options=opt)
2-但是下载“Block-image_v1.1.crx”有点棘手,因为没有直接的方法。为此,不要去:

你可以去 并将扩展url粘贴到那里,以便能够下载扩展文件


3-然后您将能够使用上述代码和您下载的扩展文件的路径。

以下是另一种禁用图像的方法:

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)
我在下面找到了:


每个人都可能想到另一种访问
chrome://settings
然后用selenium检查设置我这样开始只是出于教学好奇心,但是现在我遇到了大量的影子根元素,当你遇到超过3个影子根元素与动态内容相结合时,这显然是一种混淆并使其无法自动化的方法,尽管听起来至少在理论上可能这种方法看起来更像是一条死胡同,我将把这个答案和示例代码放在一起,纯粹是为了学习的目的,来提醒那些想参加挑战的人。。由于阴影根和动态变化,不仅很难找到内容设置,而且当您发现此时按钮不可单击时,也很难找到

driver = webdriver.Chrome()


def expand_shadow_element(element):
  shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
  return shadow_root

driver.get("chrome://settings")
root1 = driver.find_element_by_tag_name('settings-ui')
shadow_root1 = expand_shadow_element(root1)

root2 = shadow_root1.find_element_by_css_selector('[page-name="Settings"]')
shadow_root2 = expand_shadow_element(root2)

root3 = shadow_root2.find_element_by_id('search')
shadow_root3 = expand_shadow_element(root3)

search_button = shadow_root3.find_element_by_id("searchTerm")
search_button.click()

text_area = shadow_root3.find_element_by_id('searchInput')
text_area.send_keys("content settings")

root0 = shadow_root1.find_element_by_id('main')
shadow_root0_s = expand_shadow_element(root0)


root1_p = shadow_root0_s.find_element_by_css_selector('settings-basic-page')
shadow_root1_p = expand_shadow_element(root1_p)


root1_s = shadow_root1_p.find_element_by_css_selector('settings-privacy-page')
shadow_root1_s = expand_shadow_element(root1_s)

content_settings_div = shadow_root1_s.find_element_by_css_selector('#site-settings-subpage-trigger')
content_settings = content_settings_div.find_element_by_css_selector("button")
content_settings.click()
Java: 有了这个Chrome浏览器,Firefox也不会加载图像。语法不同,但参数上的字符串相同

    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);
chromeOptions=新的chromeOptions();
HashMap images=新的HashMap();
图像。放入(“图像”,2);
HashMap prefs=新的HashMap();
prefs.put(“profile.default\u content\u setting\u value”,图像);
设置实验选项(“prefs”,prefs);
驱动器=新的色度驱动器(色度选项);
firefoxOpt=新的FirefoxOptions();
FirefoxProfile profile=新的FirefoxProfile();
profile.setPreference(“permissions.default.image”,2);
firefoxOpt.setProfile(配置文件);

这在几页纸上都有效,但对于say或。