If I have a page structured like this:
<div class = "A">
<h1>AA</h1>
<p>This</p>
</div>
<div class = "A">
<h1>BB</h1>
<p>This</p>
</div>
And each time the page is loaded, there are random "class A" divs in random order with headings like: "CC", "DD", etc... How can I locate and click on the link "This" within the "BB" div? This is what I've attempted so far:
driver.findElement(By.xpath("//*[text()='BB']")).findElement(By.xpath("//*
[text()='This']")).click();
I've also tried:
WebElement name = driver.findElement(By.xpath("//*[text()='BB']"));
name.findElement(By.xpath("//*[text()='This']")).click();
However, in both cases, I always end up clicking on the "This" in the "AA" div. If the order of the divs changes randomly next time the page loads, hardcoding it to always click on the second element won't be effective.
So, I am looking for a solution to target the "This" link within the "BB" div based on the condition that "BB" exists.