Python: Disable images in Selenium Google ChromeDriver

前端 未结 3 1362
闹比i
闹比i 2020-12-12 21:02

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 o

相关标签:
3条回答
  • 2020-12-12 21:13

    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@osintstalker-HEAD@fbstalker1.py/56/selenium.webdriver.ChromeOptions.add_experimental_option

    0 讨论(0)
  • 2020-12-12 21:19

    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);
    
    0 讨论(0)
  • 2020-12-12 21:23

    There is another way that comes probably to mind to everyone to access chrome://settings and then go through the settings with selenium I started this way just for didactic curiosity, but then I hit a forest of shadow-roots elements now when you encounter more than 3 shadow root element combined with dynamic content is clearly a way to obfuscate and make it impossible to automate, although might sound at least theoretically possible this approach looks more like a dead end, I will leave this answer with the example code, just for purely learning purposes to advert the people tempted to go to the challenge.. Not only was hard to find just the content settings due to the shadowroots and dynamic change when you find the button is not clickable at this point.

    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()
    
    0 讨论(0)
提交回复
热议问题