-3

i am using selenium with java

  public void main(String args[]){
    try{
      if(ele.isDisplayed) {
        syso("test1");
      } else {
        syso("test2");
      }
    } catch(Exception e){
      syso("Exception is"+e);
    }
  }

First i will check element is visible as default i know it is not visible so
i want to go inside else statement hear
i am not going inside else condition directly getting exception,
how to go inside else condition? i tried removing try catch block please help.

  • You can put the if-else in another try-catch block within the main try block. – Sanchita Feb 25 '16 at 13:53
  • @Sanchita give me example pls – sameer joshi Feb 26 '16 at 4:57
  • -1 for writing conditional tests, i.e, tests with executation path that depends on the entry condition, is an anti-pattern. – dzieciou Feb 26 '16 at 8:04
  • @dzieciou Tell me the way with example – sameer joshi Feb 26 '16 at 8:21
  • @sameerjoshi The question is why your element is sometimes visible and sometimes not? – dzieciou Feb 26 '16 at 8:27
1

if you this in If-else way, then you have to write a separate function which will check if element is present or not. Following is the code for same.

    try {
        if(isElementDisplayed(ele)) {
            System.out.println("Test1");
        } else {
            System.out.println("Test2");
        }
    } catch (Exception e) {
        System.out.println("Exception");
    }
}

public boolean isElementDisplayed(WebElement ele) {
    boolean elementDisplayed = false;

    try {
        ele.isDisplayed();
        elementDisplayed = true;
    } catch(Exception e) {
        elementDisplayed = false;
    }

    return elementDisplayed;

}
}

In this isElementDisplayed(WebElement ele) function will test if element is present and based on that it will return the boolean value.

This function will be called from if statement.

  • Why does the first catch catches any exception of isElementDisplayed does not throw any? – dzieciou Feb 28 '16 at 18:27
  • What is the exception your're trying to catch in second catch statement? NoSuchElementException? NullPointerException? – dzieciou Feb 28 '16 at 18:27
0

If I understood your code, you are trying to go test2 when test1 fails.

You can rewrite your code as below

try {
try {
ele.isDisplayed
syso("test1")
} catch(Exception e){
syso("test2")
} catch(Exception e) {
syso("Exception is"+e);
}
  • in your code, there is no if else statement, i developed code using if else statement as you can see above, pls help – sameer joshi Feb 26 '16 at 4:55
  • @sameerjoshi for keeping your if-else statement I have added another answer please check. – Jeevan Bhushetty Feb 26 '16 at 8:03
0

It's hard to tell for sure without seeing the exception, but you're probably called isDisplayed on an element that is not present on the page (which is a subtle difference with being not displayed).

Because the element simply doesn't exist, the el.isDisplayed throws a NoSuchElementException.

You could, for example, use an ExpectedCondition. You can also split the catch block depending on the specific exception, for more custom logic. This is valid C# code but it's very similar in Java:

WebDriverWait w = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
try {
    wait.Until(ExpectedConditions.ElementIsVisible(By.Id("bubble-4")));
    syso("test1");
}
catch (Exception e) {
    // NoSuchElementException when element does not exist
    // TimeoutException when element exists but is invisible
    syso("test2: " + e.Message);
}

Edit: if you really want to hold on to your if/else statement, you will have to call a helper function that returns a boolean, and put the above logic in that helper function.

For example:

public boolean isVisible(By locator){
    try {
        WebDriverWait w = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
        wait.Until(ExpectedConditions.ElementIsVisible(locator));
        return true;
    }
    catch (Exception e){
        return false;
    }
}

Then you can just call:

if(isVisible(By.id("test"))){
    syso("test1");
}
else ...
  • in your code, there is no if else statement, i developed code using if else statement as you can see above, pls help – sameer joshi Feb 26 '16 at 7:03
  • edited my code for if/else in a clean and reusable way – FDM Feb 26 '16 at 7:06
  • it always throw an exception only so it wont go inside else condition thats why we use catch block to handle exception – sameer joshi Feb 26 '16 at 7:18
  • It doesn't throw an exception if the element is actually visible... That's what you're trying to validate, no? Anyway, do as you please. :) – FDM Feb 26 '16 at 7:26
  • No iam trying if not visible then it throws exception – sameer joshi Feb 26 '16 at 7:28
-2

Please check my updated answer

public void main(String args[]){
try{
    List<WebElement> list =driver.findElements(By.id("ads"));           
    if(list.size() > 0){            
        if(isDisplayed){
            System.out.println("TEST1");
        }else{
            System.out.println("TEST2");
        }
    }else{
        System.out.println("ELEMENT IS NOT FOUND");
    }
}catch(Exception e){
     syso("Exception is"+e);
}

}

  • is it go inside else statement ? – sameer joshi Feb 26 '16 at 5:32
  • yes, if the element is not visible.For going in to else block first of all element should be present in DOM – QAMember Feb 26 '16 at 5:35
  • your code is throwing an exception i tried using google website – sameer joshi Feb 26 '16 at 6:02
  • what is the exception ??? – QAMember Feb 26 '16 at 7:33
  • NoSuchElementException – sameer joshi Feb 26 '16 at 7:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.