Most of the time, browser automation using WebDriver is all about finding html elements: find a button and click it to perform any action, find an user input text box and set a specific text, find a link and click that links.
But sometimes, even when sounds contradictory, your test is about don’t find an element, for instance that some actions designated to an admin user are not available for a standard user.
There is a significant behavior when you are facing scenarios with nested elements (for instance divs inside divs). It seems like there is a small issue related to nesting. Let me put an example : Given the following HTML output this are the nesting observations:
<div class = "ui-dialog-buttonpane">
<div class = "ui-dialog-buttonset">
<button
role = "button"
class = "ui-button"
type = "button">
<span class = "ui-button-text">No</span>
</button>
<button
role = "button"
class = "ui-button"
type = "button">
<span class = "ui-button-text">Yes</span>
</button>
</div>
</div>
If you want to access Yes/No buttons, you first need to find the div identified by the class ui-dialog-buttonpane, then using that IWebElement, you need to find the other div with a class ui-dialog-buttonset. Coding this search, it should be something like :
//Finding modal dialog
var modalDialog = driver.FindElement(By.ClassName("ui-dialog-buttonpane"));
//finding the div that holds the buttons, but finding it within modal dialog
var buttonsDiv = modalDialog.FindElement(By.ClassName("ui-dialog-buttonset"));
//Now you can easily find the buttons within the button
Assert.IsTrue(buttonsDiv.FindElements(By.TagName("button")).count() == 2);
But this way becomes exponentially complex depending with the nesting levels, if you have 5 level nesting, you will need to follow this approach, because the WebDriver won’t find the element from the first level.
XPath becomes your best friend.
Even when there might be some hit to the performance, see Webdriver Xpath Performance, when the XPath expression becomes too long, it helps a lot to find nested elements. Following with our example to find the Yes/No buttons and using Firebug’s FirePah, see First steps with FirePath : What a useful tool , this tool helps me to get the Xpath expression to be used with WebDriver :
And if you are looking for the Xpath expression that returns the two buttons, then just edit the expression to something like this:
Note how the two buttons become selected.
One more time it’s easy to see how important is to use tools to make things easy 🙂