Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium random timeout exceptions without any message

Here is what I'm trying to do and most of the time I succeed: Basically I'm signing in on a website and then wait for a class to be in the source, then process the source code.

The exception I get:

Traceback (most recent call last):
File "foo.py", line 495, in <module>
report(login, password)
File "foo.py", line 430, in report
data = bar(login, password)
File "foo.py", line 113, in 
ui.WebDriverWait(browser, 10).until(lambda browser: browser.find_elements_by_class_name("the-class-i-want"))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until
raise TimeoutException(message)
selenium.common.exceptions.TimeoutException: Message: '' 

Here is the code:

from selenium import webdriver
import contextlib
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support.wait import WebDriverWait

with contextlib.closing(webdriver.PhantomJS('phantomjs')) as browser:
    browser.get('mywebsite')
    login_form = browser.find_element_by_id('login-form')
    email = browser.find_element_by_name('login')
    password = browser.find_element_by_name('password')
    email.send_keys(login)
    password.send_keys(password)
    password.send_keys(Keys.RETURN)
    ui.WebDriverWait(browser, 10).until(lambda browser: browser.find_elements_by_class_name("the-class-i-want"))

I tried this too:

wait_count = 0
    while wait_count < 6:
        print wait_count
        ui.WebDriverWait(browser, 10).until(lambda browser: browser.find_elements_by_class_name("the-class-i-want"))
        if browser.find_elements_by_class_name("the-class-i-want"):
            break
        wait_count += 1

I get the same exception.

I am currently trying this :

wait_count = 0
while wait_count < 6:
    try:
        ui.WebDriverWait(browser, 10).until(lambda browser: browser.find_elements_by_class_name("the-class-i-want"))
        if browser.find_elements_by_class_name("the-class-i-want"):
            break
    except:
        wait_count += 1
        continue

I haven't got to the point where it fails, I'm still testing it.

Sorry this is very long. But I'd like to find a pythonic and clean solution to those random timeouts.

Another info that could help too: the signing in process is sometimes very long, but even with a several minutes wait, it throws the exception.

like image 960
nnaelle Avatar asked Jul 02 '13 17:07

nnaelle


People also ask

How do I ignore exceptions in selenium?

Put a try-except block around the piece of code that produced that error. Show activity on this post. Show activity on this post. It looks like the browser rendering engine or Javascript engine is using the element and it is blocking other external operations on this element.

What is default selenium timeout?

Defaults to 30 seconds (or 30,000 ms). When the object is used as input for the Set Timeouts command or as part of the timeouts capability when creating a new session, all fields are optional.

How do I set selenium timeout?

setScriptTimeout(); This is used to set the amount of time the WebDriver must wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely. time – The timeout value.


1 Answers

Here is the answer I got after contacting Adam Goucher:

from selenium import webdriver
import contextlib
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support.wait import WebDriverWait


def waiter(browser):
    elements = browser.find_elements_by_class_name('the-class-i-want')
    if len(elements) != 0:
        return elements
    return False

with contextlib.closing(webdriver.PhantomJS('phantomjs')) as browser:
    browser.get('mywebsite')
    login_form = browser.find_element_by_id('login-form')
    email = browser.find_element_by_name('login')
    password = browser.find_element_by_name('password')
    email.send_keys(login)
    password.send_keys(password)
    password.send_keys(Keys.RETURN)
    ui.WebDriverWait(browser, 10).until(waiter)

And this works perfectly fine!

like image 81
nnaelle Avatar answered Sep 28 '22 13:09

nnaelle