如何从 Selenium WebDriver 的下拉列表中获取所有元素? [英] How can I get all elements from drop down list in Selenium WebDriver?
问题描述
如何从下拉列表中获取所有元素?我使用了下面的代码:
How can I get all elements from a drop down list? I used the code below:
List<WebElement> elements = driver.findElements(By.id("s"));
但我总是只得到第一个元素.
But I am always getting the first element only.
推荐答案
bindigs 中有一个专门为此设计的类.
There is a class designed for this in the bindigs.
您正在寻找 Select
类:
您需要找到"实际的 select
元素,而不是单个选项.找到那个 select
元素,让 Selenium &Select
类为您完成剩下的工作.
You would need to 'find' the actual select
element, not the individual options. Find that select
element, and let Selenium & the Select
class do the rest of the work for you.
您会寻找类似的东西(s
是实际的 select
元素):
You'd be looking for something like (s
being the actual select
element):
WebElement selectElement = driver.findElement(By.id("s");
Select select = new Select(selectElement);
Select
类有一个方便的 getOptions()
方法.这将完全按照您的想法执行.
The Select
class has a handy getOptions()
method. This will do exactly what you think it does.
List<WebElement> allOptions = select.getOptions();
现在你可以用 allOptions
做你想做的事.
Now you can do what you want with allOptions
.
这篇关于如何从 Selenium WebDriver 的下拉列表中获取所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!