任何人都可以给我发送如何验证元素的示例代码
在Selenium WebDrvier中使用Java
发布于 2013-01-18 19:05:14
为了便于理解,我使用了java print语句。
要检查元素存在的
if(driver.findElements(By.xpath("value")).size() != 0){ System.out.println(“元素存在”);}else{ System.out.println(“元素不存在”);}
或
if(driver.findElement(By.xpath("value"))!= null){ System.out.println(“元素存在”);}else{ System.out.println(“元素不存在”);}
if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){ System.out.println(“元素可见”);}else{ System.out.println("Element is InVisible");}
if( driver.findElement(By.cssSelector("a > font")).isEnabled()){ System.out.println(“元素已启用”);}else{ System.out.println(“元素已禁用”);}
if(driver.getPageSource().contains(“要检查的文本”)){System.out.println(“文本存在”);}else{ System.out.println(“文本缺失”);}
发布于 2013-01-05 02:40:20
您可以尝试如下所示:
WebElement rxBtn = driver.findElement(By.className("icon-rx"));
WebElement otcBtn = driver.findElement(By.className("icon-otc"));
WebElement herbBtn = driver.findElement(By.className("icon-herb"));
Assert.assertEquals(true, rxBtn.isDisplayed());
Assert.assertEquals(true, otcBtn.isDisplayed());
Assert.assertEquals(true, herbBtn.isDisplayed());
这只是一个例子。基本上,您声明并定义您希望使用的WebElement变量,然后Assert
它们是否显示。这是使用TestNG断言。
发布于 2015-03-16 15:06:23
下面是我的Selenium WebDriver的Java代码。编写以下方法,并在断言期间调用它:
protected boolean isElementPresent(By by){
try{
driver.findElement(by);
return true;
}
catch(NoSuchElementException e){
return false;
}
}
https://stackoverflow.com/questions/14156656
复制相似问题