List<WebElement> selectedOpts = driver.findElements(
By.cssSelector("select.form-control > option:checked"));
for(WebElement temp : selectedOpts ) {
System.out.println(temp.getText());
}
我已经执行了代码并获得了以下结果
**Result**
2tec2 Sales Network
Active
Selenium,Chennai,345/45,London Street-2
现在我想从我的选择中排除Selenium,Chennai,345/45,London Street-2。
HTML
**html code**
<div class="form-group">
<label class="control-label col-md-4 col-sm-4" for="type-
select">Category<span style="color:red">*</span></label>
<div class="col-md-8 col-sm-8">
<select defaultattr="4" class="form-control input-style mandatory"
data-val="true" data-val-number="The field CategoryID must be a number."
id="CategoryID" name="CategoryID"><option value="">--Select--</option>
<option value="1">Architectural Firm</option>
<option selected="selected" value="2">2tec2 Sales Network</option>
<option value="3">Cleaning Company</option>
</select>
<div class="form-group">
<label class="control-label col-md-4 col-sm-4" for="type-select">Company
Status</label>
<div class="col-md-8 col-sm-8">
<select class="form-control input-style" id="ddlCompanyStatus">
<option selected="selected" value="1">Active</option>
<option value="0">Non Active</option>
</select>
</div>
<div class="form-group">
<div class="form-group">
<label class="control-label col-md-4 col-sm-4" for="type-select">Company</label>
<div class="col-md-8 col-sm-8">
<select class="form-control input-style CallChange" id="ddlCompany" defaultattr="1568">
<option value="1107">2Tec2 Flooring,Chennai,Anna Salai,Raheja Towers</option>
<option selected="" value="1568">Selenium,Chennai,345/45,London Street-2</option>
<option value="1494">Test again,Wervik,Kasteelstraat 113</option>
有3个下拉列表选项,我想在下拉列表中选择前两个选定的选项文本。通过执行上面的代码,所有3个都被选中了。
答案 0 :(得分:1)
String locator = "select#CategoryID > option:checked, " +
"select#ddlCompanyStatus> option:checked"
List<WebElement> selectedOpts = driver.findElements(
By.cssSelector(locator));
for(WebElement temp : selectedOpts ) {
System.out.println(temp.getText());
}