I am using XPath/CSS and Selenium to locate elements on website. I want to create a method where I iterate through a list of locators (XPath / CSS) and program chooses whichever one works. In other words, it begins with locator one - if the locator is present it returns true and exists the loop. Otherwise it moves on to the next locator in list. Once it exhausts all CSS locators it moves on to XPath and so on.
Currently, I am thinking of implementing this as follows:
public boolean iterate(WebDriver driver, By selectorType, String[] locator)
{
driver.get("URL");
for(int selectorListCounter = 0; selectorListCounter < locator.length; selectorListCounter++) {
try
{
driver.findElement(By.(selectorType)).sendText();
System.out.println("CSS Selector: " + CSS + " found");
return true;
} catch (Exception e)
{
System.out.println(CSS + " CSS Selector Not Present");
return false;
}
}
I then plan on calling this method for each locator type (once for XPath, once for CSS etc)
Is this the best way?