如何使用 XPath 查询所有包含相同属性值的选项标签。

huangapple go评论85阅读模式
英文:

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.

&lt;div class=&quot;form-group&quot;&gt;
	    &lt;label&gt;Cards&lt;/label&gt;
	    &lt;select class=&quot;form-control giftcard-selector&quot; name=&quot;giftcard&quot;&gt;
				&lt;option data-store=&quot;Book1&quot; data-number=&quot;110&quot; data-pin=&quot;333&quot; data-balance=&quot;93.92&quot; value=&quot;81372&quot; style=&quot;display: none;&quot;&gt;Book1: 1110 ($93.92)&lt;/option&gt;
				&lt;option data-store=&quot;Book2&quot; data-number=&quot;111&quot; data-pin=&quot;0619&quot; data-balance=&quot;7.56&quot; value=&quot;81371&quot; style=&quot;display: none;&quot;&gt;Book2: 112 ($7.56)&lt;/option&gt;
				&lt;option data-store=&quot;Book1&quot; data-number=&quot;113&quot; data-pin=&quot;8229&quot; data-balance=&quot;10.24&quot; value=&quot;81369&quot; style=&quot;display: none;&quot;&gt;Book1: 113 ($10.24)&lt;/option&gt;
				&lt;option data-store=&quot;Book2&quot; data-number=&quot;114&quot; data-pin=&quot;0984&quot; data-balance=&quot;2.17&quot; value=&quot;81373&quot; style=&quot;display: none;&quot;&gt;Book2: 115 ($2.17)&lt;/option&gt;
		&lt;/select&gt;
&lt;/div&gt;

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=&quot;form-control giftcard-selector&quot;]/option@data-store=&quot;Book1&quot;

答案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=&quot;form-control giftcard-selector&quot;]/option[@data-store=&quot;Book1&quot;]

For the text:

//select[@class=&quot;form-control giftcard-selector&quot;]/option[@data-store=&quot;Book1&quot;]/text()

答案2

得分: 0

你可以使用:

cssSelector: ".form-control.giftcard-selector option[data-number='1']"

英文:

You can use:

cssSelector: &quot;.form-control.giftcard-selector option[data-number=&#39;1&#39;]&quot;

答案3

得分: 0

如果您仔细查看&lt;select&gt;元素,所有子元素&lt;option&gt;的**style**属性都设置为display: none;。因此,Selenium无法以通用方式定位它们。

解决方法

可以推测,&lt;option&gt;标签中的文本也通过&lt;ul&gt;和一些&lt;li&gt;标签表示,您需要通过这些标签与选项元素进行交互。

参考资料

您可以在以下几个相关的详细讨论中找到更多信息:

英文:

If you look at the &lt;select&gt; element closely, all the descendent &lt;option&gt; 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 &lt;option&gt; tags are also represented through a &lt;ul&gt; and a couple of &lt;li&gt; tags and you need to interact with the option elements through those tags.


Reference

You can find a couple of relevant detailed discussions in:

huangapple
  • 本文由 发表于 2020年9月8日 17:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63790924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定