**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 value="2">Interior Design Firm</option>
<option value="3">General Contractor</option>
<option selected="selected" value="4">2tec2 Sales Network</option>
<option value="5">Cleaning Company</option>
<option value="6">Commercial end user</option>
<option value="7">Distribution company</option>
<option value="8">Hotel Company</option>
</select>
我希望xpath获得选定的值(这里是上面代码中的第四个选项)。
发布于 2018-06-01 17:03:50
您希望xpath获得下拉列表中选定的选项吗?
试试这个xpath://select[@id='CategoryID']//option[@selected='selected']
您可以使用GetText()获取所选文本的文本
String text = driver.findElement(By.xpath(...)).getText();
发布于 2018-06-06 04:41:13
除了Shn的回答和你对它的评论。如果需要从5个类似的下拉列表中获取值,为什么不为每个下拉列表运行Shn 5次给定的xpath语句(通过更改xpath中的id ),并将所有这些值保存在一个数组中
String textA = driver.findElement(By.xpath(...)).getText();
String textB = driver.findElement(By.xpath(...)).getText();
String textC = driver.findElement(By.xpath(...)).getText();
String textD = driver.findElement(By.xpath(...)).getText();
String textE = driver.findElement(By.xpath(...)).getText();
String array[] = new String[] { textA, textB, textC, textD, textE };
您也可以使用循环来实现此目的。
https://stackoverflow.com/questions/50637690
复制相似问题