英文:
How to Xpath query all option tags containing the same attribute value
问题
我正在尝试查询所有带有标记为Book1的data-store值的选项标签内的文本。以下是主题下拉列表。
<div class="form-group">
<label>Cards</label>
<select class="form-control giftcard-selector" name="giftcard">
<option data-store="Book1" data-number="110" data-pin="333" data-balance="93.92" value="81372" style="display: none;">Book1: 1110 ($93.92)</option>
<option data-store="Book2" data-number="111" data-pin="0619" data-balance="7.56" value="81371" style="display: none;">Book2: 112 ($7.56)</option>
<option data-store="Book1" data-number="113" data-pin="8229" data-balance="10.24" value="81369" style="display: none;">Book1: 113 ($10.24)</option>
<option data-store="Book2" data-number="114" data-pin="0984" data-balance="2.17" value="81373" style="display: none;">Book2: 115 ($2.17)</option>
</select>
</div>
我尝试了以下xpath来查询所有带有标记为"Book1"的data-store值的选项标签内的所有文本,但返回为"Null"。非常感谢您的帮助。
//select[@class="form-control giftcard-selector"]/option[@data-store="Book1"]
英文:
I am trying to query text in within all option tags with data-store values labeled Book1. Below is the subject drop-down list.
<div class="form-group">
<label>Cards</label>
<select class="form-control giftcard-selector" name="giftcard">
<option data-store="Book1" data-number="110" data-pin="333" data-balance="93.92" value="81372" style="display: none;">Book1: 1110 ($93.92)</option>
<option data-store="Book2" data-number="111" data-pin="0619" data-balance="7.56" value="81371" style="display: none;">Book2: 112 ($7.56)</option>
<option data-store="Book1" data-number="113" data-pin="8229" data-balance="10.24" value="81369" style="display: none;">Book1: 113 ($10.24)</option>
<option data-store="Book2" data-number="114" data-pin="0984" data-balance="2.17" value="81373" style="display: none;">Book2: 115 ($2.17)</option>
</select>
</div>
I have tried the following xpath to query all text within option tags data-store values labeled "Book1" but it returns "Null".Any help would be appreciated.
//select[@class="form-control giftcard-selector"]/option@data-store="Book1"
答案1
得分: 2
我认为你的 XPath 写错了,尝试这样写:
对于元素:
//select[@class="form-control giftcard-selector"]/option[@data-store="Book1"]
对于文本:
//select[@class="form-control giftcard-selector"]/option[@data-store="Book1"]/text()
英文:
I think you built your xpath wrongly, try this:
For the elements:
//select[@class="form-control giftcard-selector"]/option[@data-store="Book1"]
For the text:
//select[@class="form-control giftcard-selector"]/option[@data-store="Book1"]/text()
答案2
得分: 0
你可以使用:
cssSelector: ".form-control.giftcard-selector option[data-number='1']"
英文:
You can use:
cssSelector: ".form-control.giftcard-selector option[data-number='1']"
答案3
得分: 0
如果您仔细查看<select>
元素,所有子元素<option>
的**style
**属性都设置为display: none;
。因此,Selenium无法以通用方式定位它们。
解决方法
可以推测,<option>
标签中的文本也通过<ul>
和一些<li>
标签表示,您需要通过这些标签与选项元素进行交互。
参考资料
您可以在以下几个相关的详细讨论中找到更多信息:
- 使用Selenium和Java自动化基于jQuery的Bootstrap下拉菜单
- 如何使用Selenium从动态下拉菜单中选择选项?
- 我想从列表中选择一个下拉选项,但HTML中没有select标签,我不确定是否应该使用Select类
英文:
If you look at the <select>
element closely, all the descendent <option>
elements are having style
attribute set as display: none;
. So Selenium won't be able to locate them in a generic way.
Solution
Presumably, the texts within the <option>
tags are also represented through a <ul>
and a couple of <li>
tags and you need to interact with the option elements through those tags.
Reference
You can find a couple of relevant detailed discussions in:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论