Matt
Matt

Reputation: 235

How to access a specific child div using xpath? (Selenium Java)

I have the following html code:

<div class="panel">
    <div class = "heading">
        <span class="wName">Name</span>
        <div class="foo1" style="display: none;"></div>
        <div class="foo2" style="display: none;"></div>
    </div>
</div>

I already located element panel and I'm trying to test when foo2 doesn't appear with the following line of code:

if (panel.findElement(By.xpath("../div[@class='foo2']")).getCssValue("display").equals("none"))

I'm not sure why this won't retrieve the element properly.

Upvotes: 4

Views: 20084

Answers (2)

phantom
phantom

Reputation: 31

How about you use descendant

panel.findElement(By.xpath("//div[@class='panel']/descendant::div[@class='foo2']"));

Source http://www.caucho.com/resin-3.1/doc/xpath.xtp#descendant

Upvotes: 1

SiKing
SiKing

Reputation: 10329

Your XPath is wrong! .. means "parent of". Single dot . would mean relative to current location.

Try: panel.findElement(By.xpath(".//div[@class='foo2']")

Upvotes: 2

Related Questions