42

Any one can send me sample code how to verify element

  1. ispresent
  2. isvisible
  3. isenable
  4. textpresent

in Selenium WebDrvier using Java

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
TEJAS TRIVEDI
  • 441
  • 1
  • 6
  • 4
  • 3
    What you have tried? Have you read any tutorials? In what language? C#? Java? PHP? Pig latin? – Arran Jan 04 '13 at 14:59
  • I want to try it in java language. Please send me tutorials or help document for selenium web driver. I will be thankful to you. please help me – TEJAS TRIVEDI Jan 04 '13 at 15:56

7 Answers7

78

I used java print statements for easy understanding.

  1. To check Element Present:

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

    Or

    if(driver.findElement(By.xpath("value"))!= null){
    System.out.println("Element is Present");
    }else{
    System.out.println("Element is Absent");
    }
    
  2. To check Visible:

    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){
    System.out.println("Element is Visible");
    }else{
    System.out.println("Element is InVisible");
    }
    
  3. To check Enable:

    if( driver.findElement(By.cssSelector("a > font")).isEnabled()){
    System.out.println("Element is Enable");
    }else{
    System.out.println("Element is Disabled");
    }
    
  4. To check text present

    if(driver.getPageSource().contains("Text to check")){
    System.out.println("Text is present");
    }else{
    System.out.println("Text is absent");
    }
    
Manigandan
  • 5,004
  • 1
  • 28
  • 47
  • 1
    Theses examples would be better using the ExpectedConditions class. – djangofan Dec 31 '13 at 19:06
  • 2
    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){ would not work as isdisplayed doesnt return false. returns true only if displayed otherwise throws element not found exception... – Manglesh Jan 19 '18 at 13:07
  • we can also find element with below code : if(!driver.findElements(By.xpath("value")).isEmpty()){ System.out.println("Element is availabe"); }else{ System.out.println("Element is not available"); } – Pankaj K. Nov 23 '22 at 12:04
12

You could try something like:

    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());

This is just an example. Basically you declare and define the WebElement variables you wish to use and then Assert whether or not they are displayed. This is using TestNG Assertions.

DarthOpto
  • 1,640
  • 7
  • 32
  • 59
  • i try above example but it is not execution next steps if previous assertion is fail. I want to use verify instead of assertion so it can run next test steps. And i also make to fail whole test case if anyof the assertion and verficatin is failed – TEJAS TRIVEDI Jan 07 '13 at 16:27
  • So what I gathered from your response is that you would like to have the test continue even if the assertion fails. Is that correct? – DarthOpto Jan 07 '13 at 21:32
  • yes i want exactly what u have written. i want to have test continue even if the test assertion is fail and in last after all test step execution mark test result as fail result in report. But it should complete all step execution. – TEJAS TRIVEDI Jan 08 '13 at 11:40
  • What language are you using? If Java are you using TestNG or JUnit? – DarthOpto Jan 08 '13 at 16:51
  • Depending on how you are running your tests the only option I could find in the TestNG documentation is the `-configurefailpolicy` for the command line parameters. http://testng.org/doc/documentation-main.html – DarthOpto Jan 09 '13 at 15:55
  • WebElement rxBtn = driver.findElement(By.className("icon-rx")); Assert.assertEquals(true, rxBtn.isDisplayed()); I think there is no point in writing the 2nd line after the first line since if the element isn't there, findElement will throw NoSuchElementException thereby ending the execution of the program at that line. assertEquals() will not be run. – learningQA Nov 17 '18 at 16:18
10

Here is my Java code for Selenium WebDriver. Write the following method and call it during assertion:

protected boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
5

Try using below code:

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;
        }
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Mayur Shah
  • 518
  • 3
  • 8
1
webDriver.findElement(By.xpath("//*[@id='element']")).isDisplayed();
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
Akongnwi Devert
  • 1,177
  • 12
  • 10
0

To make sure that an element is present you can do the following:

driver.findElements(By.id("id"));

That will return an array, if that array size is > 0 then the element/s is present.

Also, you need to provide more information, such as language and what have you tried before asking,

Good luck

  • driver.findelement(By.id("id1")).isDisplayed()' driver.findelement(By.id("id2")).sendkey("test"); Here if element is not displayed it fails test case and is not going on to next step for execution. So please help me how to handle assertion and xcontinue test execution for next steps in selenium webdrier and testng. Even i also want to mark test case fail if anyone assertion is failed. – TEJAS TRIVEDI Jan 07 '13 at 16:27
  • id is unique, you should use .findElement() for it. .findElements() is for eg. By.ClassName() – Brunis Mar 17 '15 at 11:07
-1

To check if element is visible we need to use element.isDisplayed(); But if we need to check for presence of element anywhere in Dom we can use following method

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;
    }
Jlearner
  • 573
  • 4
  • 6