如何使用 Selenium WebDriver java 从下拉列表中获取所有选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28045138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all the options from drop down using Selenium WebDriver java
提问by chandra sekhar
<select id="vehicleTypeName" class="custom-select" name="vehicleTypeName">
<option value="">-Select Style-</option>
<option value="Hatchback">Hatchback Cars</option>
<option value="Sedans">Sedan Cars</option>
<option value="MUV">MPV Cars</option>
<option value="Sport Utilities">SUV Cars</option>
<option value="Luxury Vehicles">Luxury Cars</option>
<option value="Hybrids">Hybrid Cars</option>
<option value="Minivans">Minivans</option>
<option value="Convertibles">Convertible Cars</option>
<option value="Coupe">Coupe Cars</option>
</select>
i have used this code to validate the options
我已使用此代码来验证选项
String label=driver.findElement(By.id("vehicleTypeName")).getText();
logger.info("Brand names are \t" + label );
But it is printing all the brands including "-Select Style-" this item. but I don't want to print this "-Select Style-" value.but I want to validate only brand names. please help me with this.
但它正在打印包括“-Select Style-”这个项目的所有品牌。但我不想打印这个“-Select Style-”值。但我只想验证品牌名称。请帮我解决一下这个。
回答by Jamie Rees
You could try this:
你可以试试这个:
String label=driver.findElement(By.id("vehicleTypeName"));
Select labelSelector = new Select(label);
labelSelector.deselectByValue("");
List<WebElement> allOptions = labelSelector.getOptions();
You could then use the List and it should contain all options that have something in the value
tag.
然后您可以使用 List,它应该包含value
标签中包含某些内容的所有选项。
回答by Turcia
Easy solution;
轻松解决;
String label = driver.findElement(By.id("vehicleTypeName")).getText().replace("-Select Style-", "");
String label = driver.findElement(By.id("vehicleTypeName")).getText().replace("-Select Style-", "");
BTW, it's not a good practice.
顺便说一句,这不是一个好习惯。
回答by ddavison
I just wrote a quick function for you. hope it helps!
我刚刚为您编写了一个快速功能。希望能帮助到你!
/**
* Get all <code><option/></code> innerHTML attributes
*
*/
List<String> getAllOptions(By by) {
List<String> options = new ArrayList<String>();
for (WebElement option : new Select(driver.findElement(by)).getOptions()) {
String txt = option.getText();
if (option.getAttribute("value") != "") options.add(option.getText());
}
return options;
}
Now executing:
现在执行:
getAllOptions(By.id("vehicleTypeName"));
will return:
将返回:
["Hatchback Cars", "Sedan Cars"...] // of course in List<> representation..
回答by ro.e
List<WebElement> options = driver.findElements(
By.xpath("//*[@id="vehicleTypeName"]/option"));
Now you can easily construct a new List
from options
excluding the first element:
现在您可以轻松地List
通过options
排除第一个元素来构建一个新元素:
List<String> text = new ArrayList<>();
for(int i=1; i<options.size(); i++) {
text.add(options.get(i).getText());
}
回答by Subh
Below are the possible ways to do it:
以下是可能的方法:
1- By removing the first Option from the list
1- 从列表中删除第一个选项
//Locating the Select Dropdown for Vehicle Type
Select sel = new Select(driver.findElement(By.id("vehicleTypeName")));
//Getting all the options present in the dropdown.
List<WebElement> list = sel.getOptions();
//Removing the first dropdown option from the list, i.e., '-Select Style-'
list.remove(sel.getFirstSelectedOption());
//Printing the List
System.out.println("The list without the first element, i.e., 'Select Type' is as under: ");
for(WebElement ele: list)
System.out.println(ele.getText());
2- By removing the option that has text "-Select Style-"
2- 通过删除具有文本“-选择样式-”的选项
//Locating the Select Dropdown for Vehicle Type
Select sel = new Select(driver.findElement(By.id("vehicleTypeName")));
//Getting all the options present in the dropdown.
List<WebElement> list = sel.getOptions();
//Removing the dropdown option '-Select Style-' from the list
Iterator<WebElement> iter = list.iterator();
while(iter.hasNext()){
if(iter.next().getText().equals("-Select Style-")){
iter.remove();
break;
}
}
//Printing the List
System.out.println("The list without the first element, i.e., 'Select Type' is as under: ");
for(WebElement ele: list)
System.out.println(ele.getText());
回答by Dheemanth Bhandarkar
WebElement element = driver.findElement(By.xpath("Your xpath here"));
Select select = new Select(element);
List<WebElement> list = select.getOptions();
for(int i=0; i<list.size(); i++)
{
System.out.println(list.get(i).getText());
}
All the above lines are continuous piece of code. Working : a) Point 1 and 2 are straight forward to locate a Drop down box b) In point 3, we create a List variable 'list' and stuff all the values in the drop down. c) In the for loop, we pick each element according to their index using get(i) and use getText() to acquire the text it holds.
以上所有行都是连续的一段代码。工作:a) 第 1 点和第 2 点直接定位下拉框 b) 在第 3 点,我们创建一个列表变量“列表”并填充下拉列表中的所有值。c) 在 for 循环中,我们使用 get(i) 根据它们的索引选择每个元素,并使用 getText() 获取它所保存的文本。
Hope it helps.
希望能帮助到你。
回答by Viral Singh
Can Use below use it worked for me
可以在下面使用它对我有用
Select dropDown = new Select(Driver.findElement()));
List<WebElement> e = dropDown.getOptions();
int itemCount = e.size();
for(int l = 0; l < itemCount; l++)
{
System.out.println(e.get(l).getText());
}
回答by Surya Kanta
Hi the below method will solve your problem. It will be easy for you to use
您好,下面的方法可以解决您的问题。您将很容易使用
public List<String> getAllValues() throws InterruptedException
{
Select sel = new Select(DomainName);
List<WebElement> we = sel.getOptions();
List<String> ls = new ArrayList<String>();
for(WebElement a : we)
{
if(!a.getText().equals("Select")){
ls.add(a.getText());
}
}
return ls;
}
回答by NaramukAbus
Try this one
试试这个
WebElement lelement = driver.findElement(By.id("vehicleTypeName"));
Select oSelect = new Select(lelement);
java.util.List <WebElement> elementCount = oSelect.getOptions();
int iSize = elementCount.size();
String [] arrdropdown= new String [iSize];
for (int j = 0; j < iSize; j++)
{
arrdropdown[j]=elementCount.get(j).getText();
}
logger.info("Brand names are \t" + Arrays.toString(arrdropdown));