selenium 第二次调用的操作在Firefox上不起作用

nx7onnlm  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(117)

测试使用了两次包含action的同一个函数。第一次运行时,它工作正常,测试用例通过。但是,在下一个用例中运行同一个函数时,并没有正确触发action。到目前为止,我所看到的是,程序认为该操作已经执行,因为它在下一步失败,如果前一步不工作,就无法完成下一步。
代码在Chrome和Firefox上运行。Chrome正常工作,Firefox不正常。

private Actions actions = new Actions(driver);

@FindBy(css = "div.o-dropdown:nth-child(3)")
public WebElement myAccountBtn;

public void hoverProfileGoToManageProfiles() {
   WebElement topNavBar = driver.findElement(By.cssSelector(".c-navbar__container"));
   wait.until(ExpectedConditions.invisibilityOf(loadingSpinner));
   wait.until(ExpectedConditions.elementToBeClickable(topNavBar));
   performHoverManageProfiles();
}

public void performHoverManageProfiles() {
   actions.moveToElement(myAccountBtn).perform();
   WebElement manageProfilesBtn = driver.findElement(By.xpath("//*[@id=\"app\"]/nav[1]/div/div[2]/div[1]/div[2]/div/a[1]"));
   WebElement clickableManageProfilesBtn = wait.until(ExpectedConditions.elementToBeClickable(manageProfilesBtn));
   clickableManageProfilesBtn.click();
}

正如您所看到的,在performHoverManageProfiles()中有一个WebElement,只有在操作(悬停)完成后才能找到它。
我试过将WebElement的创建移到函数中,这样每次运行时都能找到它。另外,粘贴的代码已经经过了一些修改,这就是为什么它可能会很混乱,但到目前为止,结果是一样的-在Chrome上工作,在Firefox上不工作。
还尝试在每次运行后清除myAccountBtn,并在函数开始时创建它,也没有成功。
我还想,也许这是性能的问题,也许Firefox太快,所以我添加了Thread.sleep和fluent等待,直到网站加载,但再次没有成功。

chhkpiq4

chhkpiq41#

事实证明,Actions会记住它们的状态,所以一个动作不会在一次运行中执行两次。为了减轻这种情况,输入状态必须在执行后重置,这样Actions就认为它会首先运行。

((RemoteWebDriver) driver).resetInputState();

因此,整个函数如下所示:

public void performHoverManageProfiles() {
    actions.moveToElement(myAccountBtn).perform();
    WebElement manageProfilesBtn = driver.findElement(By.xpath("//*[@id=\"app\"]/nav[1]/div/div[2]/div[1]/div[2]/div/a[1]"));
    WebElement clickableManageProfilesBtn = wait.until(ExpectedConditions.elementToBeClickable(manageProfilesBtn));
    clickableManageProfilesBtn.click();
    ((RemoteWebDriver) driver).resetInputState();
}

代码正常工作,每次运行都按预期执行第二个操作。

相关问题