Claudiu
Claudiu

Reputation: 229591

python selenium hangs on finding nonexistent elements

I'm using selenium with Python and it always freezes when I search for an element that does not exist. I've really tried everything (Firefox version 17.0.1):

>>> import selenium
>>> selenium.__version__
'2.26.0'
>>> from selenium import webdriver
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> ff = webdriver.Firefox()
>>> ff.implicitly_wait(5)
>>> ff.set_page_load_timeout(5)
>>> ff.set_script_timeout(5)
>>> waiter = WebDriverWait(ff, 5)
>>> waiter.until(lambda ff: ff.find_element_by_name("foo"))

That last command freezes indefinitely. How do I get firefox to simply return None or throw an exception when it doesn't find an element, instead of hanging forever? I'm using selenium 2.26.0

Upvotes: 1

Views: 1251

Answers (3)

root
root

Reputation: 80456

It seems to be a bug in version 2.26.0, pip install selenium==2.27.0 fixed it on my computer.

Upvotes: 1

Amey
Amey

Reputation: 8548

Based on answer found here

If you are using Firefox 17 and Selenium 2.26.0 then you are hitting defect #4814: http://code.google.com/p/selenium/issues/detail?id=4814

Upvotes: 1

Claudiu
Claudiu

Reputation: 229591

For now, I'm using this work-around:

def selenium_safe_find_element_by_name(ff, element_name):
    elements = ff.find_elements_by_name(element_name)
    if not elements:
        raise ValueError("<name=%s> not found" % (element_name,))

    return elements[0]

But it really seems that this should somehow work without that workaround.

Upvotes: 0

Related Questions