WebDriver c# wait for text example?

242 views
Skip to first unread message

Keith McIntyre

unread,
Sep 22, 2014, 5:15:43 PM9/22/14
to seleniu...@googlegroups.com
Here's what I'm trying to do (logically) -
              
// wait for done status
string status = "";
while (!status.Contains("Done")
{
  Thread.Sleep(100);
  WebElement txtStatus = driver.FindElement(By.Id("MainContent_lblActivity"));
  status = txtStatus.Text;
  Console.WriteLine(status);
}

So I'm sure there's a better way to do it using WebDriverWait and ExpectedConditions, and there are TONS of posts and web hits out there, but I'll be damned if I can get anything to work.

Does someone have a SIMPLE c# example of waiting for an element to contain specific text?  Just using the Selenium API, one method call at a time?

Thanks in advance!

Keith

Jim Evans

unread,
Sep 22, 2014, 7:10:14 PM9/22/14
to seleniu...@googlegroups.com
Your example is substantially correct, though I don't think you'd need to repeat the FindElement, unless the element is created on the fly. If you're looking for an example that uses WebDriverWait, here's one:

IWebElement element = driver.FindElement(By.Id("elementId"));
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return element.Text.Contains("Done"); });

Keith McIntyre

unread,
Sep 22, 2014, 8:16:19 PM9/22/14
to seleniu...@googlegroups.com
Jim,


Thanks!  I'll give that a try.

The issue I'm seeing with my simple loop is that the

status = txtStatus.Text;

statement hangs - sometimes.  Running under the debugger, I can break the seemingly hung loop.  I find that the above statement is entered but not returning.   But once in a while the loop detects the Done status and completes...  Very disconcerting.

I have tried it with the FindElement(...) inside and outside the while loop.  No difference...

So having wasted the afternoon on my approach, I'll give your code a try...

Keith

Keith McIntyre

unread,
Sep 22, 2014, 8:45:19 PM9/22/14
to seleniu...@googlegroups.com
Jim. 

I'm still hanging on the access to

txtStatus.Text.Contains("Done")

If I set the timeout short enough (before the web site updates the status to Done) the exception fires, so the timer is working.  But if I set a longer timeout and watch the web site display Done, the wait.Until never returns.  And if I break the debugger I'm back in the txtStatus.Text call.

So clearly I'm missing something.  I have no control over the site.  I could probably program a hairy long sleep and hope for completion, but that just seems wrong.

How else can I wait for Ajax processing to complete?

All comments welcome!

Keith
 

Jim Evans

unread,
Sep 22, 2014, 10:10:09 PM9/22/14
to seleniu...@googlegroups.com
You've glossed over some details in where you're experiencing what you describe as "hanging." Does it happen on all browsers? Just one browser? All versions of the browser? You also haven't said whether you set an implicit wait timeout (driver.Manage().Timeouts.ImplicitlyWait) anywhere. Can you provide a full code snippet from instantiation of the driver to the point that it hangs on? You say you have no control over the site being automated. That could make this extremely difficult to troubleshoot further, since the issue could be from the site itself, if a resource isn't loading properly.

For an alternative a approach, you could query the state of the AJAX library being used by the page using ExecuteScript, but you'd have to know what the features of that library are. As an example, you can check for $.active to be zero, which indicates that there are no current pending AJAX requests. Your page may use a different library, in which case you'd have to use that mechanism.

Keith McIntyre

unread,
Sep 22, 2014, 10:46:40 PM9/22/14
to seleniu...@googlegroups.com
Jim,

Firefox browser and driver...

I have set ImplicitlyWait and SetPageLoadTimeout to 300 seconds.

I added an ElementIsVisible wait before the Text.Contains("Done") test you provided.  ElementIsVisible passes.

I'm just trying to automate data collection from a commercial site I subscribe to.  I am not a web developer.  I don't have access to the source code of the site.  I know what Ajax is but know nothing about how to access Ajax libraries.  Is that really a prerequisite to using WebDriver?

I can't believe it is this hard to synchronize to a data element.

But it is what it is.  Repeatedly polling element.Text is not reliable.  Waiting.Until is unreliable.

But thanks for your help.

Keith
Reply all
Reply to author
Forward
0 new messages