java - 我已经导入了 org.openqa.selenium.interactions.Actions 但仍然抛出错误 Actions can not parsed to a variable

标签 java selenium

显示操作无法解析为变量。

我正在研究鼠标移动并创建 Actions 类的对象。我已经导入了 org.openqa.selenium.interactions.Actions。但错误仍然存​​在。我尝试了以下选项:

1.重新启动, 2.关闭和打开项目 3.刷新 4.清洁

请帮我解决这个问题

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();
        }

最佳答案

您需要添加selenium-api库到您的项目CLASSPATH为了能够使用Actions类(class)。

我强烈建议使用像 Maven 这样的构建系统或Gradle其中提供自动dependencies management所以你可以声明 selenium-javatestng 作为你的项目依赖项,其余的将由 Maven 或 Gradle 自动解析。

enter image description here

示例 Maven pom.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 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>

查看Selenium with Java文章提供了全面的信息和示例项目,您可以将其用作测试的引用或框架。

关于java - 我已经导入了 org.openqa.selenium.interactions.Actions 但仍然抛出错误 Actions can not parsed to a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052794/

相关文章:

java - Selenium WebDriver 中的无限等待

python - 如何修复尝试使用 Selenium 查找元素时引发的 TypeError?

java - Selenium在jenkins主节点上运行时出现错误 "Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms"

java - 根据对象的类型调用适当的函数

java - 无法使用 javamail api 建立连接

java - 如何动态打开/关闭springboot应用程序中的预定方法

java - 如果我只是添加一个新的子对象 java hibernate spring ,可以保存父对象吗?

python - Chrome 配置文件未加载 Selenium

unit-testing - 如何在本地或 Jenkins 上*自动化*基于浏览器的单元测试

java - 如何在 EJB JPA 中合并两个日期