如何使用Selenium设置Chromium Edge浏览器的自定义下载目录
Great question! Since Chromium Edge shares the same core engine as Chrome, setting up a custom download folder is nearly identical—you just need to use EdgeOptions instead of ChromeOptions, but the experimental options themselves are the same.
Here's how to do it step by step:
1. Use EdgeOptions instead of ChromeOptions
EdgeOptions inherits most of the same methods and experimental options from ChromeOptions, so the syntax will feel familiar to what you used for Chrome.
2. Set the download directory (and optional related settings)
You'll use the same setExperimentalOption method with the "download.default_directory" key, just like you did for Chrome. You can also add optional settings to disable download prompts or auto-open PDFs externally for a smoother test flow.
Example Code (Java)
import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; public class EdgeCustomDownloadSetup { public static void main(String[] args) { // Define your custom download path String customDownloadPath = "C:\\Users\\YourName\\Documents\\SeleniumTestDownloads"; // Initialize EdgeOptions EdgeOptions edgeOptions = new EdgeOptions(); // Set the custom download directory edgeOptions.setExperimentalOption("download.default_directory", customDownloadPath); // Optional: Disable download prompt to auto-save files edgeOptions.setExperimentalOption("download.prompt_for_download", false); // Optional: Auto-download PDFs instead of opening them in the browser edgeOptions.setExperimentalOption("plugins.always_open_pdf_externally", true); // Launch Edge with the configured options EdgeDriver driver = new EdgeDriver(edgeOptions); // Your test logic here... } }
Example Code (Python)
If you're working with Python, the approach is just as straightforward:
from selenium import webdriver from selenium.webdriver.edge.options import Options custom_download_path = "/home/yourname/selenium_test_downloads" edge_options = Options() edge_options.add_experimental_option("download.default_directory", custom_download_path) edge_options.add_experimental_option("download.prompt_for_download", False) edge_options.add_experimental_option("plugins.always_open_pdf_externally", True) driver = webdriver.Edge(options=edge_options) # Your test steps go here...
Key Notes
- Path Formatting: On Windows, use double backslashes (
\\) or raw strings to avoid escape character issues. On macOS/Linux, stick with standard forward slashes. - Driver Compatibility: Make sure your EdgeDriver version matches the version of Chromium Edge installed on your machine—mismatched versions can cause unexpected bugs.
- Experimental Options: Most Chromium-based experimental options work for Edge, so any other Chrome settings you relied on likely translate directly to EdgeOptions.
内容的提问来源于stack exchange,提问作者VishnuSajeevan

