Skip to content

finding elements sometimes cause error TypeError: object of type 'NoneType' has no len() or KeyError: 0 #65

Closed

Description

@ombre42

Sometimes the underlying call to Selenium methods such as find_elements_by_xpath return a dict with value=None. This None value is propagated all the way up to _element._element_find, where len() is used to see how many elements were found. This will cause Wait Until Page Contains Element to fail immediately, since you can't use len on None.
Unfortunately I don't have a simple test/page to reproduce, but here's a log from executing Selenium2Library.Wait Until Page Contains Element id=delivery_date_select, 1m

08:31:43.590 DEBUG POST http://127.0.0.1:50341/session/a61ed99c-c9ed-4e49-9045-8b3c7724eeac/elements {"using": "id", "sessionId": "a61ed99c-c9ed-4e49-9045-8b3c7724eeac", "value": "delivery_date_select"}
08:31:44.160 DEBUG POST http://127.0.0.1:50341/session/a61ed99c-c9ed-4e49-9045-8b3c7724eeac/elements {"using": "id", "sessionId": "a61ed99c-c9ed-4e49-9045-8b3c7724eeac", "value": "delivery_date_select"}
08:31:44.573 FAIL TypeError: object of type 'NoneType' has no len()
08:31:44.574 DEBUG Traceback (most recent call last):
File "", line 2, in wait_until_page_contains_element
File "C:\apps\Python27\lib\site-packages\Selenium2Library\keywords\keywordgroup.py", line 12, in _run_on_failure_decorator
return method(_args, *_kwargs)
File "C:\apps\Python27\lib\site-packages\Selenium2Library\keywords_waiting.py", line 63, in wait_until_page_contains_element
self._wait_until(timeout, error, self._is_element_present, locator)
File "C:\apps\Python27\lib\site-packages\Selenium2Library\keywords_waiting.py", line 71, in _wait_until
while not function(*args):
File "C:\apps\Python27\lib\site-packages\Selenium2Library\keywords_element.py", line 593, in _is_element_present
return (self._element_find(locator, True, False, tag=tag) != None)
File "C:\apps\Python27\lib\site-packages\Selenium2Library\keywords_element.py", line 515, in _element_find
if len(elements) == 0: return None

Activity

  1. ombre42 commented on Jul 22, 2012

    @ombre42
    ContributorAuthor

    Not sure if it helps but this is happening with the IE driver, and in many versions of Selenium.
    Best I can tell, the driver is violating the JsonWireProtocol because the return type for /session/:sessionId/elements is supposed to be {Array.<{ELEMENT:string}>}.

  2. Johnz86 commented on Dec 6, 2012

    @Johnz86

    This happens a lot for me, when I use webdriver and try testing on IE9.

  3. ombre42 commented on Dec 6, 2012

    @ombre42
    ContributorAuthor

    I was thinking of submitting a patch that just converts None into [], which will allow wait until page contains to work again. I think an INFO level event should be logged when that happens.

  4. ombre42 commented on Dec 8, 2012

    @ombre42
    ContributorAuthor

    Fix submitted as pull request in issue #151

  5. ombre42 commented on May 22, 2013

    @ombre42
    ContributorAuthor

    This is still occurring with Selenium 2.31 & IEDriverServer 2.32.3.0
    A bug has been reported upstream in the Selenium project.

    Here is what the failure looks like if the find returns a dict:
    06:00:58.788 DEBUG POST http://127.0.0.1:4843/session/283cb180-65d7-4d58-8431-fee1a3b4fc8f/elements {"using": "xpath", "sessionId": "283cb180-65d7-4d58-8431-fee1a3b4fc8f", "value": "id('scriptId')[.='Enter the DNIS number that shows on your phone display']"}
    06:01:01.960 FAIL KeyError: 0
    06:01:01.960 DEBUG Traceback (most recent call last):
    File "C:\Jenkins\workspace\Apollo_Login\Lib\Selenium2LibraryExt.py", line 152, in wait_until_element_visible
    lib._wait_until(timeout, error, lib._is_visible, locator)
    File "C:\Jenkins\workspace\Apollo_Login\Lib\Selenium2LibraryExt.py", line 328, in _wait_until_ignoring
    if function(*args): return
    File "c:\Apps\Python27\lib\site-packages\Selenium2Library\keywords_element.py", line 580, in _is_visible
    element = self._element_find(locator, True, False)
    File "c:\Apps\Python27\lib\site-packages\Selenium2Library\keywords_element.py", line 542, in _element_find
    return elements[0]

    Here is some captured JSON that from a different run that had the same failure:
    {"sessionId":"4cadce81-ec77-477f-83de-d3b29ffb84f4","status":0,"value":{"":null}}

  6. ombre42 commented on May 22, 2013

    @ombre42
    ContributorAuthor

    Actually not sure if the TypeError is still happening but the KeyError definitely is and that is also addressed in my pull request.

  7. emanlove commented on Dec 31, 2013

    @emanlove
    Member

    This issue was fixed under Selenium2Library version 1.3 by #172.

  8. modified the milestones: , 1.3 on Aug 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions