3

In selenium webdriver i want to use if/else condition with java. Each steps need to be checked and need to execute one by one. For example

Log.info("Clicking on Reports link");
WebElement menuHoverLink = driver.findElement(By.id("reports"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);

In this code, i need to check the id once it present it need to perform the action else it need to skip the test case not to be failed.

1
  • Am using TestNg framework Commented Jan 21, 2014 at 4:42

4 Answers 4

5
if(driver.findElement(By.id("reports").size()!=0){

   WebElement menuHoverLink = driver.findElement(By.id("reports"));
   actions.moveToElement(menuHoverLink).perform();

   }
   else{
   system.out.println("element not present -- so it entered the else loop");

}

use this one -- if the element is not persent also testcase doesnt fail

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried all the above methods but the problem when i change the ID and checked it is not throwing exception or else part it is showing Element is not currently visible and so may not be interacted with Command duration or timeout: 32 milliseconds And the test case is failed
Here is the HTML tag <a id="reports" class=" parentMenu active" name="reports" href="10.4.16.159/reports/">Reports</a>
2

Option #1:

WebElement menuHoverLink = null;
try
{
    menuHoverLink = driver.findElement(By.id("reports"));
}
catch(Exception e)
{
}
if (menuHoverLink != null)
{
    actions.moveToElement(menuHoverLink).perform();
    Thread.sleep(6000);
}

Option #2:

List<WebElement> menuHoverLinks = driver.findElements(By.id("reports"));
if (menuHoverLinks.size() > 0)
{
    actions.moveToElement(menuHoverLinks.get(0)).perform();
    Thread.sleep(6000);
}

Keep in mind that Thread.sleep by itself may also throw an exception, so you should either catch it inside the method, or add throws Exception at the end of the method's declaration.

1 Comment

There's one thing I would like to change in your Option #1. Inside catching all the exception, why not just catch ElementNotFound exception? Thoughts?
2
Try this one . i think this should work 
for me it worked 

private boolean existsElement(String id) {
        try {
            driver.findElement(By.id(id));
        } catch (Exception e) {
            System.out.println("id is not present ");
            return false;
        }

        return true;
    }


if(existsElement("reports")==true){

   WebElement menuHoverLink = driver.findElement(By.id("reports"));
   actions.moveToElement(menuHoverLink).perform();

   }
   else{
   system.out.println("element not present -- so it entered the else loop");

}

Comments

0

Use try catch block in your script like

Log.info("Clicking on Reports link");
try {
WebElement menuHoverLink = driver.findElement(By.id("reports"));
actions.moveToElement(menuHoverLink).perform();
Thread.sleep(6000);
} Catch(Exception e) {
// Element not found....do not fail my test
}

1 Comment

Will not work correctly if one of the action methods throws an exception (I'm not sure if they do), or if Thread.sleep throws an exception (I know for sure that it might). OP should put only the driver.findElement method call inside the try/catch block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.