Selenium:迭代元素列表

时间:2015-07-24 06:36:32

标签: java css selenium xpath

我使用XPath / CSS和Selenium来定位网站上的元素。我想创建一个方法,我遍历一个定位器列表(XPath / CSS),程序选择任何一个工作。换句话说,它以定位器1开始 - 如果定位器存在,则返回true并存在循环。否则它将移动到列表中的下一个定位器。一旦耗尽所有CSS定位器,它就会移动到XPath,依此类推。

目前,我正在考虑如下实施:

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;
            }


        }

然后我计划为每个定位器类型调用此方法(一次用于XPath,一次用于CSS等)

这是最好的方式吗?

2 个答案:

答案 0 :(得分:1)

我昨天确实这样做只是为了加快我的结果过程。 在没有成功和失败的情况下,您可以使用选项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 :(得分:0)

我认为以下解决方案对您有用,我不认为需要为每种类型的定位器循环一次。 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;
 }