[英]How to get value from an attribute in selenium RC in java?
I have this code for xpath and html: 我有xpath和html的这段代码:
<a class="WatchButton inicon" rel="nofollow" data-productid="111124">
xpath=/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a
How can I get the data-productid
value? 如何获得
data-productid
值?
Just add @data-productid
to the xpath expression: 只需将
@data-productid
添加到xpath表达式中:
/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a/@data-productid
Note that the xpath expression you have is very fragile since it depends on a bunch of elements and their relevant positions. 请注意,您拥有的xpath表达式非常脆弱,因为它取决于一堆元素及其相关位置。 Try to rely on the element's attributes or one of it's containers - look for id and class attributes.
尝试依赖元素的属性或其容器之一-查找id和class属性。 For example:
例如:
//a[contains(@class, "WatchButton")]/@data-productid
This gets the first link anywhere on a page that contains WatchButton
class and retrieves it's data-productid
attribute value. 这将在包含
WatchButton
类的页面上的任何位置获取第一个链接,并检索其data-productid
属性值。
* Sharing the link to the web page or showing the complete HTML could help to provide you with a more reliable xpath expression. *共享网页链接或显示完整的HTML可以帮助您提供更可靠的xpath表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.