You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium 3.141.59 Java版EdgeOptions无setExperimentalOption方法问题

问题根因

Selenium 3.141.59版本内置的EdgeOptions最初是为旧版非Chromium内核的Edge设计的,没有封装setExperimentalOption方法,新版Chromium内核Edge的实验性配置需要通过setCapability接口注入。

修复方案

直接将下载偏好配置封装后通过setCapability传入即可,完整可运行代码如下:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import java.util.HashMap;
import java.util.Map;

public class EdgeDownloadTest {
    public static void main(String[] args) {
        String location = System.getProperty("user.dir") + "\\Downloads";

        Map<String, Object> preferences = new HashMap<>();
        preferences.put("download.default_directory", location);

        // 封装Chromium内核Edge的配置项
        Map<String, Object> edgeOptionsMap = new HashMap<>();
        edgeOptionsMap.put("prefs", preferences);

        EdgeOptions options = new EdgeOptions();
        // 注入配置,key固定为ms:edgeOptions
        options.setCapability("ms:edgeOptions", edgeOptionsMap);

        System.setProperty("webdriver.edge.driver","C:\\Users\\User\\Desktop\\Selenium\\Browsers\\Edge\\msedgedriver.exe");
        WebDriver driver = new EdgeDriver(options);

        driver.manage().window().maximize();
        driver.get("https://file-examples.com/index.php/sample-documents-download/sample-doc-download/");

        driver.findElement(By.xpath("//tbody/tr[1]/td[5]/a[1]")).click();
    }
}
注意事项
  • 确保你的msedgedriver版本和本地安装的Edge浏览器主版本号完全一致,否则配置可能不生效
  • 下载路径需确保当前程序有写入权限,避免无权限导致下载失败
  • 如果可以升级依赖,推荐升级到Selenium 4.x版本,该版本的EdgeOptions已经完全适配Chromium内核Edge,自带setExperimentalOption方法,用法和ChromeOptions完全一致

内容的提问来源于stack exchange,提问作者Konstantin Sazhnev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 03:06:05