如何在Selenium 2(Selenium WebDriver)中验证元素是否存在或可见

42

请给我发送如何使用Java在Selenium WebDrvier中验证元素的示例代码。

  1. ispresent(存在)
  2. isvisible(可见)
  3. isenable(可用)
  4. textpresent(文本存在)

3
你尝试过什么?阅读过任何教程吗?使用什么语言?C#?Java?PHP?Pig Latin? - Arran
我想在Java语言中尝试它。请发送Selenium Web Driver的教程或帮助文档。我将非常感激您的帮助,请帮帮我。 - TEJAS TRIVEDI
7个回答

79

我使用Java打印语句以便更容易理解。

  1. 检查元素是否存在:

    if(driver.findElements(By.xpath("value")).size() != 0){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    

    或者

    if(driver.findElement(By.xpath("value"))!= null){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    
  2. 检查可见性:

    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
    System.out.println("Element is Visible");
    }else{
    System.out.println("Element is InVisible");
    }
    
  3. 要检查启用状态:

    if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
    System.out.println("Element is Enable");
    }else{
    System.out.println("Element is Disabled");
    }
    
  4. 检查文本是否存在

  5. if(driver.getPageSource().contains("Text to check")){
    System.out.println("Text is present");
    }else{
    System.out.println("Text is absent");
    }
    

1
这些示例使用ExpectedConditions类会更好。 - djangofan
2
如果(driver.findElement(By.cssSelector("a > font")).isDisplayed()){不起作用,因为isDisplayed不会返回false。只有在显示时才返回true,否则会抛出元素未找到的异常... - Manglesh
我们也可以使用以下代码查找元素:if(!driver.findElements(By.xpath("value")).isEmpty()){ System.out.println("元素可用"); }else{ System.out.println("元素不可用"); } - Pankaj K.

12

你可以尝试像这样:

    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断言。


我尝试了上面的例子,但如果先前的断言失败,则不会执行下一步。我想使用verify而不是assertion,这样它就可以运行下一个测试步骤。如果任何断言和验证失败,我还要使整个测试用例失败。 - TEJAS TRIVEDI
所以根据您的回复,我了解到您希望即使断言失败也能够继续进行测试。这是正确的吗? - DarthOpto
是的,我想要您所写的内容。我希望即使测试断言失败,测试仍然可以继续进行,并且在所有测试步骤执行完毕后将测试结果标记为失败结果。但它应该完成所有步骤的执行。 - TEJAS TRIVEDI
你使用的是哪种编程语言?如果是Java,你使用的是TestNG还是JUnit? - DarthOpto
根据您运行测试的方式,我在TestNG文档中找到的唯一选项是命令行参数中的-configurefailpolicy。http://testng.org/doc/documentation-main.html - DarthOpto
WebElement rxBtn = driver.findElement(By.className("icon-rx")); 如果元素不存在,findElement将抛出NoSuchElementException并在该行结束程序的执行,因此在第一行后写第二行没有意义。assertEquals()将不会运行。 - learningQA

10

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

protected boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }

5

尝试使用以下代码:

private enum ElementStatus{
        VISIBLE,
        NOTVISIBLE,
        ENABLED,
        NOTENABLED,
        PRESENT,
        NOTPRESENT
    }
    private ElementStatus isElementVisible(WebDriver driver, By by,ElementStatus getStatus){
        try{
            if(getStatus.equals(ElementStatus.ENABLED)){
                if(driver.findElement(by).isEnabled())
                    return ElementStatus.ENABLED;
                return ElementStatus.NOTENABLED; 
            }
            if(getStatus.equals(ElementStatus.VISIBLE)){
                if(driver.findElement(by).isDisplayed())
                    return ElementStatus.VISIBLE;
                return ElementStatus.NOTVISIBLE;
            }
            return ElementStatus.PRESENT;
        }catch(org.openqa.selenium.NoSuchElementException nse){
            return ElementStatus.NOTPRESENT;
        }
    }

1
webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();

0
为了确保一个元素存在,你可以执行以下操作:
driver.findElements(By.id("id"));

如果该数组的大小> 0,则将返回一个数组,然后该元素/个体就存在。

此外,您需要提供更多信息,例如语言和在提问之前尝试过的内容。

祝你好运


driver.findElement(By.id("id1")).isDisplayed()' driver.findElement(By.id("id2")).sendKeys("test");在这里,如果元素未显示,则测试用例失败,并且不会继续执行下一步。因此,请帮助我如何处理断言并继续Selenium WebDriver和TestNG的测试执行下一步。即使有任何断言失败,我也希望标记测试用例为失败。 - TEJAS TRIVEDI
id是唯一的,你应该使用.findElement()。.findElements()适用于例如By.ClassName()。 - Brunis

-1
检查元素是否可见需要使用element.isDisplayed();方法。但是,如果我们需要在Dom中检查元素的出现,则可以使用以下方法。
public boolean isElementPresentCheckUsingJavaScriptExecutor(WebElement element) {
        JavascriptExecutor jse=(JavascriptExecutor) driver;
        try {
            Object obj = jse.execute("return typeof(arguments[0]) != 'undefined' && arguments[0] != null;",
                    element);
            if (obj.toString().contains("true")) {
                System.out.println("isElementPresentCheckUsingJavaScriptExecutor: SUCCESS");
                return true;
            } else {
                System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
            }

        } catch (NoSuchElementException e) {
            System.out.println("isElementPresentCheckUsingJavaScriptExecutor: FAIL");
        }
        return false;
    }

方法execute(String, WebElement)在类型JavascriptExecutor中未定义。 - Steve Staple

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接