如何使用XPath获取属性的值 [英] How to get the value of an attribute using XPath
问题描述
我一直在使用Selenium WebDriver进行测试,并且一直在寻找XPath代码来获取HTML元素的属性值,这是我进行回归测试的一部分.但是我找不到一个好的答案.
I have been testing using Selenium WebDriver and I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. But I couldn't find a good answer.
这是我的示例html元素:
Here is my sample html element:
<div class="firstdiv" alt="testdiv"></div>
我想使用XPath获取"alt"属性的值.我有一个XPath可以使用class属性访问div元素:
I want to get the value of the "alt" attribute using the XPath. I have an XPath to get to the div element using the class attribute which is:
//div[@class="firstdiv"]
现在,我正在寻找XPath代码来获取"alt"属性的值.假设是我不知道"alt"属性的值是什么.
Now, I am looking for an XPath code to get the value of the "alt" attribute. The assumption is that I don't know what is the value of the "alt" attribute.
推荐答案
您可以使用getAttribute()
方法.
driver.findElement(By.xpath("//div[@class='firstdiv']")).getAttribute("alt");
这篇关于如何使用XPath获取属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!