首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何对下拉列表中的选定文本进行xpath

如何对下拉列表中的选定文本进行xpath
EN

Stack Overflow用户
提问于 2018-06-01 14:53:51
回答 2查看 201关注 0票数 2
代码语言:javascript
复制
**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获得选定的值(这里是上面代码中的第四个选项)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-01 17:03:50

您希望xpath获得下拉列表中选定的选项吗?

试试这个xpath://select[@id='CategoryID']//option[@selected='selected']

您可以使用GetText()获取所选文本的文本

代码语言:javascript
复制
String text = driver.findElement(By.xpath(...)).getText();
票数 1
EN

Stack Overflow用户

发布于 2018-06-06 04:41:13

除了Shn的回答和你对它的评论。如果需要从5个类似的下拉列表中获取值,为什么不为每个下拉列表运行Shn 5次给定的xpath语句(通过更改xpath中的id ),并将所有这些值保存在一个数组中

代码语言:javascript
复制
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 };

您也可以使用循环来实现此目的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50637690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档