简体   繁体   English

如何从Java的Selenium RC中的属性获取值?

[英]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.

相关问题 使用xpath无法使用Selenium RC获取属性的值 - Can't get the value of an attribute with Selenium RC using xpath 如何从中获取价值 <input> 在动态生成Value属性时,在Selenium Web驱动程序中使用java标记 - How to get the value from <input> tag using java in Selenium web driver when the Value attribute is generated dynamically 使用Selenium RC和Java从文本字段中提取值 - extract a value from a text field using selenium RC and java 如何从输入字段中获取属性值? Selenium - Java - How to get Attribute value form Input field ? Selenium - Java 如何从 selenium 中的 href 链接获取属性值 - how to get an attribute value from a href link in selenium 在Selenium WebDriver中使用Java在ID或任何属性中不存在值时如何从禁用的文本字段中获取值 - How to get value from a disabled text field when the value is not present in ID or in any attribute using Java in Selenium WebDriver 获取 selenium 中某个属性的值 - get the value of an attribute in selenium 使用Java的Selenium RC中的JavaScript - Using JavaScript from Selenium RC by Java 如何使用selenium和css获取属性值 - how to get attribute value using selenium and css 如何获取返回硒中的布尔值的属性值 - how to get an attribute value that returns a boolean in selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM