如何使用xpath来输出属性。

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

How to Use xpath to echo attributes

问题

如何使用 XPath 从以下脚本中选择并输出 data-balance 值?

    <select class="form-control giftcard-selector" name="giftcard">
    <option data-store="Your Awesome!" data-number="NUMBER0" data-pin="NUMBER1" data-balance="NUMBER2" 
    value="NUMBER2" style="display: none;">Hello World</option>
    </select>

我已经成功使用下面的 XPath 输出了 **Data-store** 值、**data-number** 值和 **data-balance** 值,结果如下,但如何仅输出 NUMBER2,即 **data-balance** 值:

    xpath=//select[@class='form-control giftcard-selector']//option   

    Result = "Your Awesome! NUMBER0 NUMBER2" 
英文:

How can i use xpath to select then echo the data-balance value from the following script?

&lt;select class=&quot;form-control giftcard-selector&quot; name=&quot;giftcard&quot;&gt;
&lt;option data-store=&quot;Your Awesome!&quot; data-number=&quot;NUMBER0&quot; data-pin=&quot;NUMBER1&quot; data-balance=&quot;NUMBER2&quot; 
value=&quot;NUMBER2&quot; style=&quot;display: none;&quot;&gt;Hello World&lt;/option&gt;
&lt;/select&gt;

I was able to use the following xpath to echo the Data-store value, the data-number value, and the data-balance value and the results are as follows but how can i echo only NUMBER2 which is the data-balance value:

xpath=//select[@class=&#39;form-control giftcard-selector&#39;]//option   

Result = &quot;Your Awesome! NUMBER0 NUMBER2&quot; 

答案1

得分: 0

尝试一下这个 -

driver.findElement(By.xpath("//select[@class='form-control giftcard-selector']/option")).getAttribute("data-balance");

希望这正是您所寻找的内容。

英文:

Try this -

driver.findElement(By.xpath(&quot;//select[@class=&#39;form-control giftcard-selector&#39;]/option&quot;)).getAttribute(&quot;data-balance&quot;);

Hope that this is what you are looking for.

答案2

得分: -1

我能够使用以下的 XPath 表达式提取所需的 data-balance 属性:

 xpath=//select[@class="form-control giftcard-selector"]/option@data-balance

结果= NUMBER2

英文:

I was able to echo the data-balance attribute i needed using the following xpath:

 xpath=//select[@class=&quot;form-control giftcard-selector&quot;]/option@data-balance

Result= NUMBER2

huangapple
  • 本文由 发表于 2020年9月6日 15:18:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761674.html
匿名

发表评论

匿名网友

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

确定