Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中从selenium RC中的属性获取值?_Selenium_Xpath - Fatal编程技术网

如何在java中从selenium RC中的属性获取值?

如何在java中从selenium RC中的属性获取值?,selenium,xpath,Selenium,Xpath,我有以下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 如何获取数据productid值?只需将@data productid添加到xpath表达式中: /html/body/div[2]/div[2]/div/div

我有以下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
如何获取数据productid值?

只需将@data productid添加到xpath表达式中:

/html/body/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[8]/a/@data-productid
请注意,您拥有的xpath表达式非常脆弱,因为它依赖于一组元素及其相关位置。尝试依赖元素的属性或其中一个容器—查找id和类属性。例如:

//a[contains(@class, "WatchButton")]/@data-productid
这将获取包含WatchButton类的页面上任意位置的第一个链接,并检索其数据productid属性值

*共享网页链接或显示完整的HTML有助于为您提供更可靠的xpath表达式