How do I resolve the “cannot access org.openqa.selenium.ContextAware” error when compiling a Selenium/Appium Maven project?

I’m working on a Selenium and Appium project for testing a PWA. Recently, compilation fails with the error:

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

I’ve tried updating Maven dependencies in pom.xml, but the issue persists. My current pom.xml uses selenium-java 4.0.0, java-client 9.4.0, and other dependencies like Cucumber and TestNG.

How can I fix the “class file for org.openqa.selenium.ContextAware not found” error and ensure my Selenium/Appium project compiles successfully?

This error usually happens when the versions of selenium-java and java-client (Appium) are incompatible.

For example, java-client 9.x requires selenium-java 4.x, but some older releases may reference removed classes like ContextAware.

Solution: Align the versions explicitly. For instance:


<dependencies>
    <!-- Selenium 4.x -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.12.0</version>
    </dependency>

    <!-- Appium Java client compatible with Selenium 4.x -->
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>9.11.0</version>
    </dependency>
</dependencies>

Then run:

mvn clean install

This often resolves the missing ContextAware class issue.

Sometimes other dependencies (like Cucumber or TestNG extensions) bring in older Selenium versions that conflict with your selenium-java dependency.

Solution: Use the <exclusions> tag to remove older Selenium jars:

<dependency>
    <groupId>io.appium</groupId>
    <artifactId>java-client</artifactId>
    <version>9.11.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Then do a fresh build:

mvn clean dependency:tree
mvn clean install

Verify only Selenium 4.x jars are included.

If you recently updated Selenium or Appium, Maven might be using cached, old jar files.

Steps:

mvn dependency:purge-local-repository
mvn clean install

This forces Maven to re-download all dependencies, ensuring org.openqa.selenium.ContextAware is present in the correct Selenium jar.