首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何验证selenium 2中存在或可见的元素(Selenium WebDriver)

如何验证selenium 2中存在或可见的元素(Selenium WebDriver)
EN

Stack Overflow用户
提问于 2013-01-04 19:50:17
回答 7查看 314.6K关注 0票数 41

任何人都可以给我发送如何验证元素的示例代码

  1. ispresent

  1. isvisible

  1. isenable

  1. textpresent

在Selenium WebDrvier中使用Java

EN

回答 7

Stack Overflow用户

发布于 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");}

  • 勾选Enable:

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(“文本缺失”);}

票数 77
EN

Stack Overflow用户

发布于 2013-01-05 02:40:20

您可以尝试如下所示:

代码语言:javascript
复制
    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断言。

票数 11
EN

Stack Overflow用户

发布于 2015-03-16 15:06:23

下面是我的Selenium WebDriver的Java代码。编写以下方法,并在断言期间调用它:

代码语言:javascript
复制
protected boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14156656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档