使用Selenium和Java获取可以从下拉列表中选择的选项列表的方法是什么?

huangapple go评论84阅读模式
英文:

What is the way to get a list of options that can be selected from a dropdown using Selenium and Java?

问题

Sure, here's the translation:

我想要通过Java在Selenium中获取一个下拉菜单中所有可选择的选项列表。我该如何做到这一点?

英文:

I want to get a list of all the options that can be selected from a dropdown menu in selenium using java. How can I do that?

答案1

得分: 1

使用这个方法

getAllSelectedOptions()

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

英文:

Use this method

getAllSelectedOptions()

Refer this site for more information
https://www.codota.com/code/java/methods/org.openqa.selenium.support.ui.Select/getAllSelectedOptions

答案2

得分: 0

以下是翻译好的内容:

使用“Select”类与下拉菜单交互时,可以使用多种方法来打印[tag:drop-down-menu]中_option_元素的文本。此外,要与所有<option>标签交互,需要使用getOptions()方法。例如,要从[tag:facebook]登录页面https://www.facebook.com/中的“Day”、“Month”和“Year”option元素中打印文本,您需要使用WebDriverWait来等待elementToBeClickable(),并且您可以使用以下定位策略

使用“id”属性从“Day”下拉菜单获取选项:

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

使用[tag:xpath]和[tag:java-8]的stream()map()从“Month”下拉菜单获取选项:

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]和[tag:java-8]的stream()map()一行代码从“Month”下拉菜单获取选项:

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, ..., 1907, 1906, 1905]
英文:

There are multiple approaches to print the texts from the option elements of a [tag:drop-down-menu]. Ideally while interacting with a [tag:html-selct] you need to use the Select Class. Further to interact with all the &lt;option&gt; tags you need to use getOptions() method. As an example to print the texts from the Day, Month and Year option elements within [tag:facebook] landing page https://www.facebook.com/ you you need to use WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies.

Options from Day Dropdown using id attribute:

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

Options from Month Dropdown using [tag:xpath] and [tag:java-8] stream() and map():

Select selectMonth = new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//select[@id=&#39;month&#39;]&quot;))));
List&lt;String&gt; myMonths = selectMonth.getOptions().stream().map(element-&gt;element.getText()).collect(Collectors.toList());
  System.out.println(myMonths);

Console Output:

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

Options from Month Dropdown using [tag:css_selectors] and [tag:java-8] stream() and map() in a single line of code:

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

Console Output:

[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

得分: 0

这种方法可能会帮助您理解如何从下拉菜单中获取所有可选选项。

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()); });
英文:

This approch might help you to get understand how to get all selectable options from dropdown

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

huangapple
  • 本文由 发表于 2020年7月25日 13:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63084835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定