英文:
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?
<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>
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='form-control giftcard-selector']//option
Result = "Your Awesome! NUMBER0 NUMBER2"
答案1
得分: 0
尝试一下这个 -
driver.findElement(By.xpath("//select[@class='form-control giftcard-selector']/option")).getAttribute("data-balance");
希望这正是您所寻找的内容。
英文:
Try this -
driver.findElement(By.xpath("//select[@class='form-control giftcard-selector']/option")).getAttribute("data-balance");
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="form-control giftcard-selector"]/option@data-balance
Result= NUMBER2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论