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

这篇博客探讨了在使用Selenium进行自动化测试时遇到的一个挑战:如何处理短暂出现并快速消失的三级导航元素。作者分享了一段代码,尝试通过鼠标按下和移动来点击二级导航后迅速出现的三级导航,但遇到了ElementNotVisibleException。解决方案是使用Selenium的条件等待,确保元素可见后再进行点击操作,以避免此类异常。
摘要由CSDN通过智能技术生成

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值