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

标签 java selenium selenium-webdriver

有一个下拉列表,我需要将新值与旧月份字符串= 2015 年 9 月(非常规井)进行比较。如果不等于,那么这是一个新的月份,应该下载它,否则退出循环。我可以将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)";

最佳答案

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

祝你好运。

<小时/>

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

/**
 * 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 - 如何使用 selenium web driver/java 将 List<webelement> 放入 ArrayList 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370946/

相关文章:

java - 避免 "No mapping found for HTTP request with URI"或 catch-all Controller

java - SQL语法错误异常 : ORA-00911: invalid character

java - 如何使用 Selenium 在 Indeed 上单击“立即申请”按钮?

Python Selenium 检查 html 元素是否存在

selenium - 如何在codeception chrome headless 浏览器中忽略ssl证书?

c# - 在新标签页中打开链接 selenium c#

java - struts2 Web 应用程序中的 I18n 重定向问题

java - 在 JAVA 中格式化 Date.toString() 输出

java - 查找正确的输入嵌套类 - Java - Selenium Webdriver

java - 通过xpath获取webelement的文本