Reputation: 23
to clarify a really messy title: There is a piece of page code that looks like this (I have simplified the actual code):
<div class="CLASS1">
<p>...
<p>
<strong>
<i CLASS2 "></i>
THE DATA I WANT
</strong>
</p>
<p>...
<p>...
</div>
I am using Selenium Webdriver with Java and I've been trying to get the "DATA I WANT"
line, but since it is contained between tags (and not the CLASS2 italic tag), I am having hard time accomplishing this. Note that the rest of the paragraphs (marked as <p>...
) contain the analogous constructions.
Currently it seems like searching for a CLASS1 <strong>
tag that contains CLASS2 element <i>
might be the solution, but I have not been able to compose a correct search path so far. Now I am not sure if I am approaching this problem from a correct angle at all.
Any suggestions are greatly aprpeciated! I would like to have the shortest and the most reliable solution for this...
Upvotes: 0
Views: 297
Reputation: 3630
For the HTML you have posted. Following CSS selector should work.
div.CLASS1 > p:nth-child(2) > strong
If you need to get the the strong
tag that contains i.CLASS2
then you could do something like this.
// get all p tags
List<WebElement> pTags = driver.findElements(By.cssSelector("div.CLASS1 > p"));
WebElement myWebElement = null;
// iterate and find p tag that contains i.CLASS2
for (WebElement element : pTags) {
if (element.findElements(By.cssSelector("i.CLASS2")).size() == 1) {
myWebElement = element.findElement(By.cssSelector("strong"));
break;
}
}
// the data you want
System.out.println(myWebElement.getText());
Upvotes: 4