I am new to Selenium webdriver, maybe this question is obvious. I am after situation like this:

If the element exists, click it and go back to index page:

driver.findElement(By.id("...."])).click();

if doesn't exit, skip it and go back to index page. The test still goes on without any exception thrown.

I know one solution to this:

driver.findElements( By.id("...") ).size() != 0

so i tried:

if(driver.findElements(By.id("....")).size() > 0)
    {
        driver.findElement(By.id("....")).click();
        driver.findElement(By.cssSelector("...")).click();
    } 
else
    {
        driver.findElement(By.cssSelector("....")).click();
    }

This turned out really ugly though because if I have 10 elements to verify, this IF condition needs to be written 10 times.

Any workaround to make it neat?

Edit
Report