option:checked")-6ren">
gpt4 book ai didi

java - 如何从选择(下拉)中排除元素

转载 作者:行者123 更新时间:2023-12-02 11:09:14 24 4
gpt4 key购买 nike

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 个都被选中。

最佳答案

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

关于java - 如何从选择(下拉)中排除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com