如何用Python Selenium的ChromeOptions默认启用Flash及内置插件?
Hey there, I’ve got you covered for setting up Chrome to auto-enable Flash and all built-in plugins—no manual clicks needed, just like your Firefox setup. Here’s the practical Python solution using Selenium:
First, a quick heads-up: Modern Chrome versions (87 and above) have officially dropped Flash support. If Flash is critical for your automation, you’ll need to use Chrome version 86 or earlier (along with a matching ChromeDriver) that still includes the Flash plugin. With that out of the way, let’s jump into the code:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Initialize ChromeOptions to tweak browser settings chrome_options = Options() # Define preference settings to auto-enable plugins and Flash prefs = { # Enable all built-in plugins by default "profile.default_content_setting_values.plugins": 1, # Allow Flash content to run automatically without prompts "profile.default_content_setting_values.flash": 1, # Explicitly enable the Shockwave Flash plugin "plugins.plugins_enabled": ["Shockwave Flash"], # Optional: Enable external PDF viewer (adjust based on your specific plugin needs) "plugins.always_open_pdf_externally": True } # Attach the preferences to ChromeOptions chrome_options.add_experimental_option("prefs", prefs) # Add command-line flags to enforce plugin functionality chrome_options.add_argument("--enable-plugins") chrome_options.add_argument("--allow-outdated-plugins") # Optional: Use if working with legacy plugin versions # Launch Chrome with the configured settings driver = webdriver.Chrome(options=chrome_options) # Test by navigating to your target site with plugin/Flash content driver.get("https://your-target-website.com")
Quick Explanations:
- Flash Compatibility: Don’t forget the version note—newer Chrome builds won’t recognize Flash settings at all, so stick to v86 or earlier if Flash is non-negotiable.
- Plugin Defaults: Setting
profile.default_content_setting_values.pluginsto1ensures every built-in plugin starts enabled, no manual toggling required. - Command-Line Flags:
--enable-pluginsmakes sure Chrome’s plugin system is active, while--allow-outdated-pluginshelps bypass blocks for older plugin versions that Chrome might flag.
This setup mirrors your Firefox profile configuration, letting you run automated sessions without dealing with any plugin permission pop-ups.
内容的提问来源于stack exchange,提问作者Dhiraj Jha

