Java 我们如何使用 selenium webdriver 将 weblist 元素存储到数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34892868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:05:53  来源:igfitidea点击:

How we store weblist element into array using selenium webdriver

javaselenium

提问by Kumar

How we store web list element into array using Selenium WebDriver

我们如何使用 Selenium WebDriver 将网络列表元素存储到数组中

eg:

例如:

weblist.get(j).findElement(By.className("accordion-toggle")).getText()

It contains list of element how we store that elements into array.

它包含我们如何将这些元素存储到数组中的元素列表。

回答by parishodak

Assuming you know the number of elements in the list, use Java's List interface:

假设您知道列表中的元素数量,请使用 Java 的 List 接口:

List<String> list = new ArrayList<String>();

for(j=0;j<weblist.size();J++){  
  list.add(weblist.get(j).findElement(By.className("accordion-toggle")).getText())
}

回答by Sanjay Bhimani

Instead of use findElementyou have to use findElementsmethod of selenium webdriver. It will directly return list of web elements. To use By functionality of selenium create custom method getBy which will use By abstract class.

findElement您必须使用findElementsselenium webdriver 的方法,而不是使用。它将直接返回网页元素列表。要使用 selenium 的 By 功能,请创建自定义方法 getBy,它将使用 By 抽象类。

public List<WebElement> findElements(String locator, long... timeOut) {
    try {
        if (timeOut.length == 1 && timeOut[0] == 0) {
            return driver.findElements(getBy(locator));
        } else if (timeOut.length == 1 && timeOut[0] > 0) {
            waitForPresent(locator, timeOut[0]);
        } else {
            waitForPresent(locator);
        }
    } catch (Exception e) {

    }
    return driver.findElements(getBy(locator));
}

String xPath = "xpath=//*[@text='some text']";
//String xPath = "name='some text'";
//String xPath = "id=xxxx";

private By getBy(String locator) {
    locator = getProps().getString(locator, locator);
    String[] parts = locator.split("=", 2);
    By by = null;
    switch (parts[0].trim()) {
    case "xpath":
        by = By.xpath(parts[1]);
        break;
    case "name":
        by = By.name(parts[1]);
        break;
    case "link":
        by = By.linkText(parts[1]);
        break;
    case "id":
        by = By.id(parts[1]);
        break;
    case "css":
        by = By.cssSelector(parts[1]);
        break;
    default:
        throw new RuntimeException("invalid locator");
    }
    return by;
}

回答by murali selenium

You can try like this

你可以这样试试

    //to catch all web elements into list
    List<WebElement> myList=driver.findElements(By.className("accordion-toggle"));

    //myList contains all the web elements
    //if you want to get all elements text into array list
    List<String> all_elements_text=new ArrayList<>();

    for(int i=0; i<myList.size(); i++){

        //loading text of each element in to array all_elements_text
        all_elements_text.add(myList.get(i).getText());

        //to print directly
        System.out.println(myList.get(i).getText());

    }

Thank You

谢谢你

回答by Fai Hasan

using weblist.size() helps when the end result it unknown. Thus help us create array list, without giving a definite end.

当最终结果未知时,使用 weblist.size() 会有所帮助。从而帮助我们创建数组列表,而不是给出一个明确的结束。

for(j=0;j<weblist.size();J++){