<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>
我已使用此代码验证选项
String label=driver.findElement(By.id("vehicleTypeName")).getText();
logger.info("Brand names are \t" + label );
但它正在打印所有品牌,包括&#34; - 选择风格 - &#34;这个项目。但是我不想打印这个&#34; - 选择风格 - &#34; value.but我想只验证品牌名称。请帮帮我。
答案 0 :(得分:5)
我刚刚为你写了一个快速功能。希望它有所帮助!
/**
* 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;
}
现在执行:
getAllOptions(By.id("vehicleTypeName"));
将返回:
["Hatchback Cars", "Sedan Cars"...] // of course in List<> representation..
答案 1 :(得分:3)
可以使用以下使用它对我有用
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());
}
答案 2 :(得分:1)
简单的解决方案;
String label = driver.findElement(By.id("vehicleTypeName")).getText().replace("-Select Style-", "");
答案 3 :(得分:1)
List<WebElement> options = driver.findElements(
By.xpath("//*[@id="vehicleTypeName"]/option"));
现在,您可以轻松地从List
构建一个新的options
,不包括第一个元素:
List<String> text = new ArrayList<>();
for(int i=1; i<options.size(); i++) {
text.add(options.get(i).getText());
}
答案 4 :(得分:1)
您好以下方法将解决您的问题。您可以轻松使用
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;
}
答案 5 :(得分:0)
你可以试试这个:
String label=driver.findElement(By.id("vehicleTypeName"));
Select labelSelector = new Select(label);
labelSelector.deselectByValue("");
List<WebElement> allOptions = labelSelector.getOptions();
然后您可以使用列表,它应该包含value
标记中包含所有内容的所有选项。
答案 6 :(得分:0)
以下是可行的方法:
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-删除包含文字的选项&#34; - 选择样式 - &#34;
//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());
答案 7 :(得分:0)
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());
}
以上所有行都是连续的代码。 工作: a)第1点和第2点直接找到下拉框 b)在第3点,我们创建一个List变量'list'并填充下拉列表中的所有值。 c)在for循环中,我们使用get(i)根据索引选择每个元素,并使用getText()获取它所拥有的文本。
希望它有所帮助。
答案 8 :(得分:0)
尝试这个
setvbuf(stream, buf, _IOFBF, size)
答案 9 :(得分:0)
//**Selecting all Option Using tagName as Option in List and run foreach loop**
for(WebElement Element:driver.findElements(By.tagName("option")))
{System.out.println(Element.getText());}