Python:在 Selenium Google ChromeDriver 中禁用图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28070315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Disable images in Selenium Google ChromeDriver
提问by 1man
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 ChromeDriverworks in Java. So we should do the same thing in Python:
1- Disable images in Selenium Google ChromeDriver 中的答案适用于 Java。所以我们应该在 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
2- 但是下载“Block-image_v1.1.crx”有点棘手,因为没有直接的方法可以做到这一点。为此,而不是去: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.
您可以访问http://chrome-extension-downloader.com/并粘贴扩展 URL 以下载扩展文件。
3- Then you will be able to use the above mentioned code with the path to the extension file that you have downloaded.
3- 然后,您将能够使用上述代码和您下载的扩展文件的路径。
回答by rocky qi
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:
我在下面找到了它:
回答by Eduard Florinescu
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.
每个人都可能想到另一种方法来访问chrome://settings
,然后使用 selenium 进行设置我以这种方式开始只是为了说教的好奇心,但是当您遇到超过 3 个阴影根时,我现在遇到了阴影根元素的森林元素与动态内容相结合显然是一种混淆并使其无法自动化的方法,尽管听起来至少在理论上可能这种方法看起来更像是一个死胡同,我会将这个答案与示例代码一起留下,仅用于纯粹的学习目的广告人们被诱惑去挑战。. 当您发现此时按钮不可点击时,不仅由于 shadowroots 和动态变化而很难找到内容设置。
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()
回答by user3453444
Java:With this Chrome nor Firefox would load images. The syntax is different but the strings on the parameters are the same.
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);