解决selenium的EdgeOptions addArguments is not supported问题

先说明一下原因:selenium依赖错误、网络连接问题导致无法下载edge驱动。

一、依赖错误

先贴上selenium自动化配置代码,这份代码可以自动搜索"迪丽热巴":

java 复制代码
import org.openqa.selenium.By;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

public class firstTest {

    void searchTest() throws InterruptedException {

         //使⽤插件管理⼯具webdrivermanager
//         WebDriverManager.edgedriver().setup();//这句注释的原因是因为网络问题,改成自己下载的本地文件
         System.setProperty("webdriver.edge.driver", "D:\\360Downloads\\edge downloads\\webDriver\\msedgedriver.exe");

         //添加浏览器配置
         EdgeOptions options = new EdgeOptions();
         //1)允许任何来源的远程连接

          options.addArguments("--remote-allow-origins=*");
//         options.setCapability("args", Arrays.asList("--remote-allow-origins=*"));
         //创建浏览器驱动对象
         EdgeDriver driver = new EdgeDriver(options);
         //访问百度⽹⻚,搜索"⽐特科技"
         driver.get("https://www.baidu.com");
         Thread.sleep(3000);
         driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("⽐特科技");
         Thread.sleep(3000);
//         driver.findElement(By.cssSelector("#su")).click();
         //退出
         driver.quit();
         }


}

​

pom.xml配置如下:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>bit</groupId>
    <artifactId>training</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>training</name>
    <description>training</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.25.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.3</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
            <dependencies>
                <!-- 强制所有 Selenium 模块使用 4.25.0 版本 -->
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-api</artifactId> <!-- 补充这行! -->
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                    <version>4.25.0</version>
                </dependency>

                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-chrome-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-edge-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-firefox-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-ie-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-remote-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-safari-driver</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-support</artifactId>
                    <version>4.25.0</version>
                </dependency>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-json</artifactId>
                    <version>4.25.0</version>
                </dependency>

                <!-- 保留 Spring Boot 依赖管理 -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>

               
            </dependencies>


    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>bit.training.TrainingApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

使用selenium实现自动化测试时,我的第一个问题是找不到addArguments这个方法。

复制代码
options.addArguments("--remote-allow-origins=*");

经查询,得知是我引入的selenium是4.25.0的版本(4以上的版本都可以),但是在idea内置的maven依赖树那里引入的是selenium的3的版本,因为selenium3的版本属于旧版本,还没有引入addArguments方法,所以导致错误。当遇到 "EdgeOptions addArguments is not supported" 问题,通常是因为用错了 EdgeOptions 的类,或者使用了旧版本 / 不兼容的 Selenium/EdgeDriver。

为什么引入的是4的版本,但是实际上依赖的是3的版本呢?原因在于:

我一开始没强制 selenium-api 的版本,Maven 会默认使用 Spring Boot 传递依赖带来的旧版本(3.141.59),导致 "其他模块是 4.x,唯独 api 是 3.x" 的冲突。

附上检查依赖树的步骤:

解决方法就是在pom文件中,强制所有selenium版本一致(见我上述pom代码的dependencyManagement标签),刷新maven依赖并清除idea的缓存。

二、网络问题

复制代码
//使⽤插件管理⼯具webdrivermanager
一开始我使用下面这句命令
  WebDriverManager.edgedriver().setup();
发现代码报错:

查询原因,得知失败原因:Apache HttpClient(WebDriverManager 底层用于网络请求的组件)在尝试与 msedgedriver.azureedge.net建立连接时,连接对象(conn)未初始化就被调用,导致空指针异常。

msedgedriver.azureedge.net 是微软 Edge 驱动的官方下载源,国内访问可能存在不稳定或被拦截的情况,需优先确认网络可用性

方案 1:检查网络连接(最可能解决)

msedgedriver.azureedge.net 是微软 Edge 驱动的官方下载源,国内访问可能存在不稳定或被拦截的情况,需优先确认网络可用性:

  1. 测试源地址可达性
    • 打开浏览器,访问 https://msedgedriver.azureedge.net/LATEST_STABLE(获取最新稳定版驱动版本号)。
    • 若无法打开 / 超时:说明网络无法访问该源,需通过 科学上网更换国内镜像源 解决。
    • 经过访问网址,发现就是因为网络无法访问资源。此时,我采用了方案2,手动下载驱动并指定路径。
  2. 关闭防火墙 / 杀毒软件
    • 临时关闭电脑的防火墙(Windows Defender 或第三方杀毒软件),避免其拦截 Java 程序的网络请求。
  3. 配置代理(若需科学上网)
    • 若使用代理,需在 Java 程序中配置代理参数,确保 WebDriverManager 能通过代理访问外部网络。示例代码(在 setup() 前添加):

      java

      运行

      复制代码
      // 配置 HTTP/HTTPS 代理(替换为你的代理地址和端口)
      System.setProperty("http.proxyHost", "127.0.0.1");
      System.setProperty("http.proxyPort", "1080");
      System.setProperty("https.proxyHost", "127.0.0.1");
      System.setProperty("https.proxyPort", "1080");
方案 2:手动下载驱动并指定路径(绕开自动下载)

若网络问题无法解决,可直接手动下载驱动,避免 WebDriverManager 自动下载的过程:

  1. 获取本地 Edge 版本
    • 打开 Edge 浏览器,输入 edge://settings/help,查看版本号(如 140.0.3485.94)。
  2. 下载对应版本驱动
  3. 解压并指定驱动路径
    • 将解压后的 msedgedriver.exe 放到本地路径(如 D:\drivers\msedgedriver.exe)。

    • 在代码中直接指定驱动路径,跳过 WebDriverManager 调用: java

      运行

      复制代码
      // 替换原有的 WebDriverManager.setup() 代码
      System.setProperty("webdriver.edge.driver", "D:\\drivers\\msedgedriver.exe");
      WebDriver driver = new EdgeDriver(); // 直接初始化驱动
复制代码
//System.setProperty("webdriver.edge.driver", "D:\\360Downloads\\edge downloads\\webDriver\\msedgedriver.exe");

至此,问题得到解决,可以正常使用selenium进行自动化测试了

相关推荐
万粉变现经纪人19 小时前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
gc_229920 小时前
学习Python中Selenium模块的基本用法(18:使用ActionChains操作鼠标)
python·selenium
卓码软件测评20 小时前
第三方软件登记测试机构:【软件登记测试机构HTML5测试技术】
前端·功能测试·测试工具·html·测试用例·html5
可可南木21 小时前
ICT 数字测试原理 3 --SAFETYGUARD 文件
开发语言·测试工具·pcb工艺
卓码软件测评1 天前
第三方软件测试公司:【Gatling基于Scala的开源高性能负载测试工具】
测试工具·开源·scala·压力测试·可用性测试·第三方软件测试
paid槮2 天前
selenium完整版一览
selenium·测试工具
gc_22992 天前
学习Python中Selenium模块的基本用法(17:使用ActionChains操作键盘)
python·selenium
njxiejing2 天前
基于GNS3 web UI配置RIP协议(Wireshark 分析)
网络·测试工具·wireshark
卓码软件测评2 天前
K6的CI/CD集成在云原生应用的性能测试应用
前端·功能测试·测试工具·ci/cd·云原生