找到你要的答案

Q:Retrieve value from within a div tag in xpath

Q:在XPath中一个div标签检索价值

I am trying to retrieve the value in the data-appid field, I have tried using following-sibling but its not really a sibling per se. Not sure how to go about retrieving this. Any pointers will really be great.

<div class="section app" data-appid="532054761" data-updateid="10184169">

我试图在数据检索值AppID的领域,我已经尝试使用下面的兄弟但不是一个真正的兄弟本身。不知道如何去检索。任何指针都会很棒。

<div class="section app" data-appid="532054761" data-updateid="10184169">
answer1: 回答1:

The sibling axis applies to elements, not attributes.

You can reference data-appid simply as an attribute of the div element. For example,

//div/@data-appid

will select 532054761

If you need to be more specific about the particular div element for which you want its data-appid, you can use a predicate to select a particular div element. For example:

//div[@data-updateid='10184169']/@data-appid

同级轴适用于元素,而不是属性。

你可以简单的AppID的div元素属性的参考数据。例如,

//div/@data-appid

将选择532054761

如果你需要为希望其数据AppID特定div元素更具体的,你可以使用一个谓词来选择一个特定的元素。例如:

//div[@data-updateid='10184169']/@data-appid
answer2: 回答2:

You need to use getAttribute() method

Try the following code

String dataAppId = driver.findElement(By.xpath("//div[@class='section app']")).getAttribute("data-appid");
System.out.println(dataAppId);

你需要使用getattribute()方法

尝试以下代码

String dataAppId = driver.findElement(By.xpath("//div[@class='section app']")).getAttribute("data-appid");
System.out.println(dataAppId);
xpath