如何使用 selenium web driver/java 将 List<webelement> 放入 ArrayList?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34370946/
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 15:35:25  来源:igfitidea点击:

How to put List<webelement> into ArrayList using selenium web driver/java?

javaseleniumselenium-webdriver

提问by user2762008

There is a drop downlist, where i need to compare the new values with a old month string= Sep 2015 (Unconventional wells). Then if not equals then that is a new month and it should be downloaded, or else come out of the loop. Can I put the List into ArrayList and compare the string?

有一个下拉列表,我需要将新值与旧月份字符串 = 2015 年 9 月(非常规井)进行比较。然后如果不等于那么这是一个新的月份,它应该被下载,否则就会退出循环。我可以将 List 放入 ArrayList 并比较字符串吗?

Please help

请帮忙

WebDriver driver=new FirefoxDriver();
          //opening the PA page
          driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
        //maximizing the window
          driver.manage().window().maximize();
          WebElement select = driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));



          //List<WebElement> options = select.findElements(By.tagName("option"));

          String str="Sep 2015 (Unconventional wells)";

回答by Jeremiah

method below will pull out the current options of the dropdown to a List.

下面的方法会将下拉列表的当前选项拉出到列表中。

Best of Luck.

祝你好运。



Pull the current 'Reporting Period'content to a List

将当前“报告期”内容拉入列表

/**
 * Uses a {@link FirefoxDriver} to load the targeted website and extracts all current options of the 'Reporting
 * Period' drop down to a List of String references.
 * 
 * @return List of String references.
 */
private List<String> getCurrentReportingPeriodContent() {
    // List to hold the value we will return to the caller.
    List<String> currentOptions = new ArrayList<>();

    WebDriver webDriver = new FirefoxDriver();
    webDriver.get(
        "http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
    // maximizing the window
    webDriver.manage().window().maximize();

    // This is the 'By.xpath' lookup used to find the dropdown field
    By reportingPeriodLookup = By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl03_ddValue']");
    // Find the 'Reporting Period' drop down on the page.
    WebElement select = webDriver.findElement(reportingPeriodLookup); // Find the drop down

    // Pull out the options as web elements
    List<WebElement> matches = select.findElements(By.tagName("option"));

    // Traverse the web elements to extrat the text. Text gets added to the 'currentOptions' List
    for (WebElement match : matches) {
        currentOptions.add(match.getText());
    }

    // Clean up the webdriver
    webDriver.close();

    // return the List of Strings pulled out of the 'options' back to the caller.
    return currentOptions;
}