4

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.

2 Answers 2

2

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

alternatively, you can use the cleaner CSS solution. @OP. By.cssSelector("div.foo2")
@sircapsalot If we are going after "clean", then I think By.className("foo2") wins out.
or of course we can go the "less clean" route. By.cssSelector("div[class~='panel']>div[class~='heading']>div[class]:nth-child(3)") ;)
Actually, since the div of interest isn't a direct child of the panel, the XPath needs to be .//div[@class='foo2'], or ./div/div[@class='foo2'], or ./*/div[@class='foo2'], etc.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.