I use XPath / CSS and Selenium to search for elements on a website. I want to create a method in which I repeat the list of locators (XPath / CSS) and the program selects what works. In other words, it starts with a locator - if a locator is present, it returns true and there is a loop. Otherwise, it moves to the next locator in the list. As soon as he runs out of all CSS locators, he moves on to XPath, etc.
I am currently going to implement 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;
}
}
Then I plan to call this method for each type of locator (once for XPath, once for CSS, etc.)
Is this the best way?
source
share