Selenium MoveToElement 不起作用,但测试用例正在通过

Selenium MoveToElement does not work but test cases are passing

在下面的场景中,我尝试从主菜单中选择 select 选项 4-1。用过moveToElement()、click()。当我执行我的脚本时,用例显示为通过,但我没有看到 window 中的幻灯片出现,这是单击“选项 4-1”后的预期行为。

代码:

public class CreateppPage extends PageFactory {

private WebDriver driver;
private WebDriverWait wait;
private Actions act;
/**
      Selectors section
 */

@FindBy(xpath = "//div[@class='Main Button']")
private WebElement AddMenu;
@FindBy(xpath="//li[@class='item-submenu']//span[contains(text(),'Option4')]")
private WebElement subMenu;
@FindBy(xpath="//span[contains(text(),'Option4-1')]")
private WebElement subsubMenu;

/*****************Methods section***********************/

public CreateppPage(WebDriver driver, long wait) {
    
    this.driver = driver;
    this.wait = new WebDriverWait(driver,wait);
    initElements(driver, this);
    driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
}

public void createMpp() {
    
    wait.until(ExpectedConditions.visibilityOf(AddMenu));
    act = new Actions(driver);
    try {
        Thread.sleep(2000);
        act.moveToElement(AddMenu).click().build();
        Thread.sleep(1000);
        act.moveToElement(subMenu).click().moveToElement(subsubMenu).click().build();
        Thread.sleep(10000);    
    }
    catch(Exception e) {
        System.out.println(e.getCause());
    }
}
}

你不见了.perform()
应该是

act.moveToElement(AddMenu).click().build().perform();
Thread.sleep(1000);
act.moveToElement(subMenu).moveToElement(subsubMenu).click().build().perform();
Thread.sleep(10000);    

我还建议减少甚至删除硬编码延迟。
应改用显式等待。

wait.until(ExpectedConditions.visibilityOfElementLocated(AddMenu));
act.moveToElement(AddMenu).click().build().perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(subsubMenu));
act.moveToElement(subMenu).moveToElement(subsubMenu).click().build().perform();

我可能会这样做:

new Actions(driver).moveToElement(AddMenu).click().moveToElement(subsubMenu).click().build().perform();
moveToElement() Moves the mouse to the middle of the element.

Build(): creates the chain of the actions that need to be 
performed

perform(): perform those chain of actions which is build by using 
Action build method.

Internally the perform() called the build() method so if we call 
the build().perform() explicitly so we are calling .build() twice

So it seems like you miss the .perform()

act.moveToElement(subMenu).click()
.moveToElement(subsubMenu).click().build().perform();

OR

act.moveToElement(subMenu).click()
.moveToElement(subsubMenu).click().perform();

.perform() : A convenience method for performing the actions without calling build() first.