Selenium: Iterating through a list of items

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?

+4
source share
2 answers

, . 1 2.

1 . , while 8 ( ). , , , , . , , , .

, , , , .

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
int checkForElement=1;
success = false; failure = false;
        while (driver.findElements(By.className("successMessage")).size()==0 && driver.findElements(By.className("errormessage")).size()==0 && checkForElement<=4 )
        {
            checkForElement+=1;
        }
        checkForElement=1;//reset variable
        try
        {
            @SuppressWarnings("unused")//suppresses the warning so that the class is clean
            WebElement successful = driver.findElement(By.className("successMessage"));//not used practically, but logically used to look for the presence of an element without waiting the normal implicit wait time I would have at 6 seconds
            success = true;
        }
        catch(Exception e)
        {
            success = false;
        }
        try
        {
            @SuppressWarnings("unused")//suppresses the warning so that the class is clean
            WebElement failing = driver.findElement(By.className("errormessage"));//not used practically, but logically used to look for the presence of an element without waiting the normal implicit wait time I would have at 6 seconds
            failure = true;
        }
        catch(Exception e)
        {
            failure = false;
        }

        if(success)
        {
            //run success code
        }
        else if(failure)
        {
            //run failure code
        }
+1

, , . findElement() , ,

 //Define as many locators as you like
 private static final By cssLaunchIcon() {return By.css("div.myDemoItemActions i.launchIcon");}
 private static final By cssLaunchIcon1() {return By.css("div.myDemoItemActions i.launchIcon.a");}
 private static final By cssLaunchIcon2() {return By.css("div.myDemoItemActions i.launchIcon.li");}
 private static final By xpathLauncIcon() {return By.xpath("//ul/li/a[text()='launch']");}
 private static final By idLaunchIcon() {return By.id("launchIcon");}

 //Initialise the non empty list of locators
 List<By> locators= Arrays.asList(cssLaunchIcon(), xpathLauncIcon(), idLaunchIcon(), cssLaunchIcon1(), cssLaunchIcon2());

 public booolean findElement(List<By> locators) {
    Iterator<By> locs = locators().iterator();
    boolean found = false;
    //while iterator has another locator && no locator found yet
    while(locs.hasNext && !found) {
    WebElement el = locs.next();
    try{
      if(driver.findElement(el).isDisplayed) {
        found = true
      }
    } catch(NoSuchElementException e) {
        System.out.println("Could not find " + el)
   }
   return found;
 }
0

Source: https://habr.com/ru/post/1599454/


All Articles