【发布时间】:2013-05-22 00:33:50
【问题描述】:
如何从下拉列表中获取所有元素?我使用了下面的代码:
List<WebElement> elements = driver.findElements(By.id("s"));
但我总是只得到第一个元素。
【问题讨论】:
-
你能粘贴它的html代码吗
标签: java selenium selenium-webdriver drop-down-menu html-select
如何从下拉列表中获取所有元素?我使用了下面的代码:
List<WebElement> elements = driver.findElements(By.id("s"));
但我总是只得到第一个元素。
【问题讨论】:
标签: java selenium selenium-webdriver drop-down-menu html-select
娜美塔,
使用以下代码,您将在选择框中获得可用选项列表,然后单击其中一个。
列表选项 = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals("male")) {
option.click();
break;
}
}
【讨论】:
在绑定中有一个为此设计的类。
您正在寻找Select 类:
您需要“找到”实际的select 元素,而不是单个选项。找到 select 元素,然后让 Selenium 和 Select 类为您完成剩下的工作。
你会寻找类似的东西(s 是实际的select 元素):
WebElement selectElement = driver.findElement(By.id("s");
Select select = new Select(selectElement);
Select 类有一个方便的 getOptions() 方法。这将完全按照您的想法进行。
List<WebElement> allOptions = select.getOptions();
现在您可以使用allOptions 为所欲为。
【讨论】:
使用这个希望对你有帮助。
List WebElement allSuggestions = driver.findElements(By.xpath("Your Xpath"));
for (WebElement suggestion : allSuggestions)
{
System.out.println(suggestion.getText());
}
【讨论】:
这将有助于列出下拉列表中的所有元素:
Select dropdown = new Select(driver.findElement(By.id("id")));
//Get all options
List<WebElement> dd = dropdown.getOptions();
//Get the length
System.out.println(dd.size());
// Loop to print one by one
for (int j = 0; j < dd.size(); j++) {
System.out.println(dd.get(j).getText());
}
【讨论】:
List<WebElement> featureList;
featureList = Locate your Element;
for (WebElement i : featureList) {
System.out.println("\n********************** " + i.getText());
}
【讨论】:
WebElement ele=driver.findElement(By.xpath(".//*[@id='month']"));
List<WebElement> x = ele.findElements(By.tagName("option"));
for(WebElement ele1:x) {
String y=ele1.getAttribute("innerHTML");
System.out.println(y);
}
【讨论】:
String [] ex = {"A","b","c","d"};
List<WebElement> Str = driver.findElements(By.xpath("//*[@id=\"institutionName\"]/option"));
for (int i = 0; i < ex.length; i++) {
System.out.println(Str.get(i).getText());
Assert.assertEquals(ex[i], Str.get(i).getText());
}
【讨论】:
Select drop = new Select(driver.findElement(By.id("id")));
List<WebElement> dd = drop.getOptions();
System.out.println(dd.size());
for (int j = 0; j < dd.size(); j++) {
System.out.println(dd.get(j).getText());
}
【讨论】:
这是一种获取所有下拉列表并返回列表的方法。
public List getDropDownList(String Xpath) {
WebElement dropdowns = driver.findElement(By.xpath(Xpath));
Select select = new Select(dropdowns);
List<String> dropdown = new ArrayList<String>();
List<WebElement> allOptions = select.getAllSelectedOptions();
Iterator<WebElement> itr = allOptions.iterator();
while (itr.hasNext()) {
String st = itr.next().getText();
dropdown.add(st);
}
return dropdown;
}
希望对你有所帮助。
【讨论】:
我以 Facebook 的注册页面为例,可能有助于理解。
这是从月份下拉列表中获取所有月份名称选项的代码。
List<WebElement> option = driver.findElements((By.xpath("//[@id='month']/option")));
ArrayList a = new ArrayList();
for (WebElement str: option)
{
String s = str.getText();
if(!s.equals("Month")) {
a.add(s);
}
else {
continue;
}
}
System.out.println("The Output is: "+ a);
上面代码的解释是,
- 将所有元素存储在一个列表中。
- 声明一个空的数组列表来存储选项。
- 通过使用for每个循环将所有选项一一提取并存储到ArrayList中。
- 将所有选项打印为列表。
希望这会对你有所帮助。! 祝你好运!!
【讨论】:
有多种方法可以从drop-down-menu 的option 元素打印文本。理想情况下,在与 html-select 元素交互时,您需要使用 Select 类。进一步与<option>标签进行交互,您需要使用getOptions()方法。
例如打印来自 Day、Month 和 Year 元素的文本 option facebook 登陆页面 @987654338 @你需要诱导WebDriverWait为elementToBeClickable(),可以使用下面的Locator Strategies。
使用id属性:
代码块:
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());
使用xpath 和java-8 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]
在一行代码中使用 [tag:css_selectors] 和 java-8 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]
【讨论】: