简体   繁体   中英

I have imported org.openqa.selenium.interactions.Actions but still throwing error Actions can not resolved to a variable

Showing Actions can not be resolved to a variable.

I am working on Mouse movement and creating object of Actions class.I have already imported org.openqa.selenium.interactions.Actions. But still the error exist.I have tried following options:

1.Restart, 2.Close and open project 3.Refresh 4.Clean

Please help me into this

package storeFront;
    import org.testng.annotations.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.interactions.Actions;

public class WithTestNG {


@Test(priority = 0)
        public void OpenStore() {
    String exePath = "C:\\Users\\Downloads\\chromedriver_win32\\chromedriver.exe";
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Downloads\\chromedriver_win32\\chromedriver.exe" );
            WebDriver driver = new ChromeDriver();

            String URL = "https://facebook.com";

            driver.get(URL);

            Actions action = new Actions(driver);
            action.moveToElement(driver.findElement(By.xpath("a#top-bar-menu.search-dropdown.ng-binding")).build().perform();
        }

You need to add selenium-api library to your project CLASSPATH in order to be able to use Actions class.

I would strongly recommend using a build system like Maven or Gradle which provides automatic dependencies management so you could declare selenium-java and testng as your project dependencies and the remaining ones would be automatically resolved by Maven or Gradle.

在此处输入图像描述

Example Maven pom.xml file:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>java-selenium-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
        </dependency>
    </dependencies>

</project>

Check out Selenium with Java article for comprehensive information and example project which you can use as the reference or skeleton for your tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM