首页 > 解决方案 > Selenium Java Provar - 为什么我的动作 moveToElement 不执行

问题描述

我正在尝试在 Provar 中使用 Selnium Java 执行 moveToElement 操作。我的行动进口:

import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

我的自定义方法是:

        public void checkContractNumberOnHover() {
        WebDriver driver = provarSeleniumDriver.getWebDriver();
        Actions builder = new Actions(driver);

        WebElement contract = driver.findElement(By.xpath("MyXpath1"));
        WebElement contractProperNumber = driver.findElement(By.xpath("MyXpath2"));

        Action mouseOverContract = builder.moveToElement(contract).build();
        mouseOverContract.perform();
value");

        assertTrue(contractProperNumber.isDisplayed());

        String contractActualString = contractProperNumber.getText();
        assertTrue(contractActualString.contains("N2019-0001"));        
    }
}

我的测试需要将鼠标悬停在工具提示上并读取然后比较值(值仅在悬停时可见)。悬停似乎我的测试甚至没有悬停在第一名的项目上。在日志中有 xpath2 错误的信息(没有元素导致工具提示未显示。我正在尝试使用本教程中的操作: https ://www.guru99.com/keyboard-mouse-events-文件-webdriver.html

PS 我的 xpaths ok 测试了多次,为它们中的每一个找到一个元素。我不知道为什么 moveToElemnt 动作甚至没有触发:(

标签: javaseleniumhovertooltip

解决方案


将 xpath 的每个发现放入Try -catch--see. 如果未找到元素也即将到来xpath1

Actions actions = new Actions(driver);
try{
  WebElement contract = driver.findElement(By.xpath("MyXpath1"));
}
catch(Exception e)
{
syso("print exception"+e.getmessage);
}

WebElement contractProperNumber = driver.findElement(By.xpath("MyXpath2"));
actions.moveToElement(contract).`enter code here`perform();
System.out.println("Done Mouse hover on xpath1");

推荐阅读