karthik c v
karthik c v

Reputation: 21

Web Element retrieval using inner Web Element attribute in selenium Java

<div class="Happy">  --->1
    <p class="desc">
    <p>
</div>
<div class="Happy"> --->2
    <p class="asc">
    </p>
</desc>

How can I get the second WebElement in selenium

Upvotes: 1

Views: 34

Answers (1)

frianH
frianH

Reputation: 7563

Locate the element by xpath like below:

//div[@class='Happy' and .//p[@class='asc']]

Get the inner by .getAttribute("innerHTML").

Then, you can split the result using new line .split("\n"), and it seem like the target is available at the first index:

String str = driver.findElement(By.xpath("//div[@class='Happy' and .//p[@class='asc']]")).getAttribute("innerHTML").split("\n")[0];
System.out.println(str);

Upvotes: 1

Related Questions