【问题标题】:What is the way to get a list of options that can be selected from a dropdown using Selenium and Java?获取可以使用 Selenium 和 Java 从下拉列表中选择的选项列表的方法是什么?
【发布时间】:2020-11-14 23:37:46
【问题描述】:

我想使用 java 从 selenium 的下拉菜单中获取所有选项的列表。我该怎么做?

【问题讨论】:

标签: java selenium drop-down-menu java-stream html-select


【解决方案1】:

使用这个方法

getAllSelectedOptions()

请参阅此网站以获取更多信息 https://www.codota.com/code/java/methods/org.openqa.selenium.support.ui.Select/getAllSelectedOptions

【讨论】:

    【解决方案2】:

    有多种方法可以从option 元素打印文本。理想情况下,在与 交互时,您需要使用 Select 类。进一步与所有 <option> 标签进行交互,您需要使用 getOptions() 方法。例如,打印来自 DayMonthYear 的文本 option 元素内 登陆页面 https://www.facebook.com/ you you elementToBeClickable() 需要使用WebDriverWait,您可以使用以下Locator Strategies

    使用 id 属性的 Day Dropdown 选项:

    WebElement dayElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("day")));
    Select selectDay = new Select(dayElement);
    List<WebElement> dayList = selectDay.getOptions();
    for (int i=0; i<dayList.size(); i++)
        System.out.println(dayList.get(i).getText());
                  
    

    使用 stream()map() 的月份下拉列表中的选项:

    Select selectMonth = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='month']"))));
    List<String> myMonths = selectMonth.getOptions().stream().map(element->element.getText()).collect(Collectors.toList());
      System.out.println(myMonths);
    

    控制台输出:

    [Month, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]
              
    

    Month Dropdown 选项在一行代码中使用 [tag:css_selectors] 和 stream()map()

    System.out.println(new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select#year")))).getOptions().stream().map(element->element.getText()).collect(Collectors.toList()));
    

    控制台输出:

    [Year, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905]
    

    【讨论】:

      【解决方案3】:

      此方法可能会帮助您了解如何从下拉列表中获取所有可选选项

      driver.navigate().to("https://the-internet.herokuapp.com/");
      WebDriverWait wait = new WebDriverWait(driver,10,100); 
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/dropdown']"))).click();    
      WebElement element = driver.findElement(By.xpath("//select[@id='dropdown']"));
      Select select = new Select(element);    
      List<WebElement>allOptions = select.getOptions();
              allOptions.forEach(value->{System.out.println(value.getText());});
      

      【讨论】:

        猜你喜欢
        • 2022-12-15
        • 2020-08-02
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多