Common problems with Selenium: cannot find elements due to page not loading fast enough

(With massive apologies to everyone that attended my talk, I totally failed to teach the ``implicitly_wait`` api correctly, how embarassing!)

You may occasionally see errors like this:

AttributeError: 'NoneType' object has no attribute 'text'

In this case, I was trying to look at body.text to check whether a page contained some text. What goes wrong is that, sometimes, selenium tries to look at the page before it's finished loading. Thankfully, there's a solution: the implicitly_wait function

It should be called like this:

self.browser.implicitly_wait(3)

where self.browser is a webdriver instance, the implicitly_wait is a function call (not an attribute!). The parameter is in seconds.

Once you've set that, selenium will automatically keep polling for up to 3 seconds, if it can't find a DOM element you're asking for.

More info at:

http://seleniumhq.org/docs/04_webdriver_advanced.html

Meta

Published: July 9, 2012

Author: harry

Comments:  

Word Count: 125

Next: The next stage: writing a book

Previous: Tutorial discussion on Hacker News

Tags

None

Article Links

  1. WebDriver: Advanced Usage — Selenium Documentation
Comments powered by Disqus