Selenium:遍历元素列表 [英] Selenium: Iterate Through Element List

查看:113
本文介绍了Selenium:遍历元素列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XPath/CSS 和 Selenium 来定位网站上的元素.我想创建一个方法,在其中迭代定位器列表(XPath/CSS),然后程序选择任何一个有效的方法.换句话说,它从定位器 1 开始 - 如果定位器存在,则返回 true 并存在循环.否则它会移动到列表中的下一个定位器.一旦它用完所有 CSS 定位器,它就会移动到 XPath 等等.

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


        }

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

I then plan on calling this method for each locator type (once for XPath, once for CSS etc)

这是最好的方法吗?

推荐答案

实际上我昨天这样做只是为了加快我的结果处理过程.你有选项 1 和选项 2,而不是成功和失败.

I actually did this yesterday just to speed up my result process. Where instead of success and failure you have Option 1 and Option 2.

在此程序中,每次检查元素的隐式等待时间为 1 秒.所以 while 循环总共持续 8 秒(因为它每次迭代生成两个数组).这通过将元素放入数组来检查元素是否存在,然后检查数组的大小,如果元素不存在,则数组将为空.但是,当这些条件之一失败时,则意味着您的元素之一在页面上退出.

The implicit wait time is 1 second per check for element in this program. So the while loop lasts 8 seconds total (as it makes two arrays per itteration). This checks for the presence of an element by putting it into an array and then checks the size of the array, it the element isn't present the array will be empty. However when one of these conditions fails it means one of your elements exits on the page.

然后我们在它下面设置布尔值来找出页面上存在哪些元素并捕获不存在的错误.

Then below it we set booleans to find out which of these elements was present on the page and catch the error of the one that doesn't exist.

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
        }

这篇关于Selenium:遍历元素列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆