duckus
duckus

Reputation: 213

Selenium 2 WebDriver does not evaluate updated DOM as expected

I am using Selenium 2 WebDriver in C# to automate an input page. There is a validation summary control that is hidden when the DOM is loaded. Here's how it looks on additional load.

<div id='validation_summary' style='display: none'>
  <div id='validation_message'/>

When my pageObject is initialized in Selenium, the Displayed property of the WebElement is set to 'false'. This I would expect.

After clicking a submit button, the dom is changed to:

<div id='validation_summary' style='display: block'>
  <div id='validation_message'>
     <div><a>Fix Me</a></div>
  </div>

When I try to access the WebElement in the following manner, after the click to validate is issued, the Displayed property is still 'false' and the appended div is not visible:

var validation = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until<IWebElement>((d)=>d.FindElement(By.Id('validation_message'));

Should this actually work? Should I expect Selenium to be savvy enough to evaulate the DOM in its current state? How does it actually evaluate the Displayed property of an element? If I look at the PageSource property, I do see the text is present. I am not seeing why I don't see my changes reflected by Selenium.

Upvotes: 2

Views: 1726

Answers (2)

Frigik
Frigik

Reputation: 449

Try this:

var scripter = driver as JavaScriptExecutor;

var result = scripter.ExecuteScript("return $('#validation_message').is(':hidden');").ToString();
if (result.ToLower().Equals("true"))
{
//Do your stuff
}

And don't shy to use javaScript. To my humble opinion Selenium still has not enough tools, so you can implement your own just using javaScript.

Upvotes: 2

djangofan
djangofan

Reputation: 29689

Yes, my answer is using Java pseudo-code, but Selenium is smart enough to always get the latest version of the element. During this loop, the WebElement instance will continue to refresh itself, so its not stale:

@FindBy( id = 'validation_message')
public WebElement validatorMessageElement;
...
PageFactory.initObjects()
...
for ( infiniteLoop ) {
   boolean isThere = validatorMessageElement.isDisplayed();
   sleep(1 second);
}

Upvotes: 0

Related Questions