2

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.

duckus
  • 213
  • 3
  • 15
  • have you tried running it yet? I don't see a problem with the logic. I guess this depends alot on the specifics of your page. How long the new
    is visible is a pretty relevant concern. Have you checked with your DOM viewer to see what the hidden property was set to as well and how long it takes to become visible? It might be that selenium is locating the element too quickly right before it becomes visible. Try writing a wait_for_visible() method that should help you
    – Greg Aug 24 '12 at 03:08
  • I believe I've tried waiting for it to become visible, but it timed out. I'll try that again. – duckus Aug 24 '12 at 15:27

2 Answers2

2

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.

michal krzych
  • 411
  • 5
  • 19
Frigik
  • 449
  • 1
  • 3
  • 13
0

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);
}
djangofan
  • 28,471
  • 61
  • 196
  • 289