【发布时间】:2016-06-08 22:31:40
【问题描述】:
我想先验证是否启用了分页链接。如果它 已启用然后单击链接,否则我不想单击 链接并跳过 JAVA 中的 if 条件。
我正在使用 selenium 网络驱动程序和 TestNG。
谢谢, 瑞希尔·巴特
【问题讨论】:
-
您能否在链接“启用”和“禁用”时发布 HTML 代码。我试图理解你所说的“启用”是什么意思。通常它要么存在要么不存在,你不能真正禁用它。
我想先验证是否启用了分页链接。如果它 已启用然后单击链接,否则我不想单击 链接并跳过 JAVA 中的 if 条件。
我正在使用 selenium 网络驱动程序和 TestNG。
谢谢, 瑞希尔·巴特
【问题讨论】:
public void elementIsDisable(WebDriver driver, By by) throws Exception {
WebElement disable = driver.findElement(by);
Thread.sleep(2000);
String disableAttribute = disable.getAttribute("disabled");
Assert.assertEquals("true", disableAttribute);
}
然后在你的测试中调用方法:
homePage.elementIsDisable(driver, By.id("acc-confirm"));
这个检查webElement是否被禁用
【讨论】:
使用浏览器的 web 控制台检查元素来检查是否有任何属性可以使用样式、类等禁用链接。
String temp = selenium.getAttribute(locator@attribute);
现在您可以验证 temp 的值了。
也请参考:http://www.seleniumtests.com/2011/06/selenium-tutorial-get-attribute-for.html
【讨论】:
使用 isEditable(locator),如果链接被禁用,则返回 false,否则返回 true
【讨论】:
if(existsElement("test")==true){
WebElement menuHoverLink = driver.findElement(By.id("test"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
}
else{
System.out.println("element not present -- so it entered the else loop");
}
【讨论】: