如何在 Selenium 2 API 中处理鼠标悬停

发布于 2024-11-07 04:44:26 字数 1275 浏览 3 评论 0原文

String strPrimaryNav = "MEN";
String strSecondaryNav = "Shoes";
String strTertiaryNav = "Golf";

driver.findElement(By.linkText(strPrimaryNav)).click();

WebElement weSecNav = driver.findElement(By.className("secondaryButton").linkText(strSecondaryNav));

Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//just trying with for loop as the tertiary popup disappears quickly and hence getting ElementNotVisible Exception
for (int i = 0 ; i< 2; i++){
    mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
    //mouse.mouseMove(((Locatable)weSecNav).getCoordinates(), 0, 0 );
    //WebElement weTerNav = driver.findElement(By.className("tertiaryButton").linkText(strTertiaryNav));
    WebElement weTerNav = driver.findElement(By.linkText(strTertiaryNav));
    boolean isSecDisplayed = ((RenderedWebElement)weTerNav).isDisplayed();
    System.out.println("isDisplayed: " + isSecDisplayed);
    System.out.println(" " + ((RenderedWebElement)weSecNav).getAttribute("href"));
    System.out.println(" " + ((RenderedWebElement)weSecNav).getValueOfCssProperty("action"));
    weTerNav.click();
}

我正在尝试使用 selenium 2 编写下面的代码,但是,第三级弹出窗口不会停留很长时间来单击它,因此在第三级单击时会出现 ElementNotVisible 异常。

String strPrimaryNav = "MEN";
String strSecondaryNav = "Shoes";
String strTertiaryNav = "Golf";

driver.findElement(By.linkText(strPrimaryNav)).click();

WebElement weSecNav = driver.findElement(By.className("secondaryButton").linkText(strSecondaryNav));

Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
//just trying with for loop as the tertiary popup disappears quickly and hence getting ElementNotVisible Exception
for (int i = 0 ; i< 2; i++){
    mouse.mouseDown(((Locatable)weSecNav).getCoordinates());
    //mouse.mouseMove(((Locatable)weSecNav).getCoordinates(), 0, 0 );
    //WebElement weTerNav = driver.findElement(By.className("tertiaryButton").linkText(strTertiaryNav));
    WebElement weTerNav = driver.findElement(By.linkText(strTertiaryNav));
    boolean isSecDisplayed = ((RenderedWebElement)weTerNav).isDisplayed();
    System.out.println("isDisplayed: " + isSecDisplayed);
    System.out.println(" " + ((RenderedWebElement)weSecNav).getAttribute("href"));
    System.out.println(" " + ((RenderedWebElement)weSecNav).getValueOfCssProperty("action"));
    weTerNav.click();
}

I was trying the below code using selenium 2 but, the tertiary popup not stays long to click it and hence getting ElementNotVisible exception at Tertiary click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-11-14 04:44:26

您至少可以在发送点击之前检查该元素是否可见:

Selenium s = new WebDriverBackedSelenium( driver, URL );
s.waitForCondition( "!( document.getElementById('.....') === null )", "20000" );
s.click( .... );

这可以避免出现异常。我不确定是否有办法让弹出窗口停留的时间比应有的时间长。

You can at least check that the element is visible before you send your click:

Selenium s = new WebDriverBackedSelenium( driver, URL );
s.waitForCondition( "!( document.getElementById('.....') === null )", "20000" );
s.click( .... );

This avoids the exception. I'm not sure there is a way to make the popup stay any longer than it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文