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

编译Selenium/Appium Maven项目时遇cannot access org.openqa.selenium.ContextAware错误求助

编译报错:无法访问org.openqa.selenium.ContextAware类

我正在用Selenium和Appium开发PWA测试项目,之前一切正常,现在编译时出现以下错误:

cannot access org.openqa.selenium.ContextAware
class file for org.openqa.selenium.ContextAware not found

尝试修改pom.xml依赖但未解决问题,当前pom.xml配置如下:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>automation-framework</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>16</source>
          <target>16</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>7.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-testng</artifactId>
      <version>7.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.12.0</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.5.18</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <version>7.0.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.appium</groupId>
      <artifactId>java-client</artifactId>
      <version>9.4.0</version>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.11.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

错误原因分析

  • 版本不兼容:Appium Java Client 9.4.0依赖的Selenium版本远高于你使用的4.0.0,ContextAware类在Selenium 4.x后续版本中存在结构变动,低版本Selenium无法满足Appium 9.x的依赖要求
  • 依赖冲突:Maven依赖传递可能引入了不匹配的Selenium包,导致编译时找不到目标类
  • 编译器版本混乱:pom中maven-compiler-plugin配置的source/target为16,但properties中设置的是17,编译环境版本不一致可能引发类加载问题

解决方法

  1. 统一Selenium与Appium版本
    Appium Java Client 9.4.0官方推荐搭配Selenium 4.15.0及以上版本,修改selenium-java的版本:

    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.20.0</version>
    </dependency>
    
  2. 清理并更新Maven依赖
    执行命令强制清理本地缓存并重新拉取依赖:

    mvn clean install -U
    
  3. 统一编译器版本
    修改maven-compiler-plugin的配置,与properties中的版本保持一致:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>17</source>
        <target>17</target>
      </configuration>
    </plugin>
    
  4. 排查依赖传递冲突
    执行命令查看依赖树,确认是否有其他依赖引入了旧版本Selenium,如有则排除:

    mvn dependency:tree
    

    例如发现某依赖引入了Selenium 4.0.0,可通过<exclusions>标签排除:

    <dependency>
      <!-- 有冲突的依赖 -->
      <exclusions>
        <exclusion>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:54:52