【问题标题】:How to put List<webelement> into ArrayList using selenium web driver/java?如何使用 selenium web 驱动程序/java 将 List<webelement> 放入 ArrayList?
【发布时间】:2015-12-19 12:50:07
【问题描述】:

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

请帮忙

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)";

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

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

    祝你好运。


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

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

    【讨论】:

    • 你现在可能在笑。这是用java写的吗?请帮我写java代码
    • 我只需要在 webdriver :(
    • 我们可以在 webdriver 中试试这个吗?你有很大的帮助
    • 很抱歉给您带来了困惑。我已经提取了对我建议您应用的方法的响应。您需要将驱动程序设置、url 和 xpath 回填到组合框。
    • 我想我理解的是,您想知道自上次运行代码以来添加到下拉列表中的任何新内容。对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多