orange1
orange1

Reputation: 2939

Selenium not recognizing user input in Python

I am working through a book repository, specifically chapter 5 of Test-Driven Development, which can be found at this repository: https://github.com/hjwp/book-example/tree/chapter_05 . When I attempt to run the functional tests, which create a simple POST form, submit some input, and then check to see whether the input has been rendered, I get the following error:

======================================================================
FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "book-example-chapter_05/functional_tests.py", line 45, in test_can_start_a_list_and_retrieve_it_later
    self.check_for_row_in_list_table('1: Buy peacock feathers')
  File "book-example-chapter_05/functional_tests.py", line 18, in check_for_row_in_list_table
    self.assertIn(row_text, [row.text for row in rows])
AssertionError: '1: Buy peacock feathers' not found in ['']

---------------------------------------------------------------------

It seems from a discussion on the mailing list that this might be due to the browser not receiving the 'ENTER' key. When I change the line to inputbox.send_keys('Buy peacock feathers\n'), to directly send a new line character, I get the following error:

======================================================================
ERROR: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "book-example-chapter_05/functional_tests.py", line 44, in test_can_start_a_list_and_retrieve_it_later
    inputbox.send_keys(Keys.ENTER)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 322, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webelement.py", line 448, in _execute
    return self._parent.execute(command, params)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 196, in execute
    self.error_handler.check_response(response)
  File "/Users/r/virtualenvs/r/bin/python3venv/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9348)
    at Utils.getElementAt (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/[email protected]/components/command-processor.js:8942)
    at fxdriver.preconditions.visible (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/[email protected]/components/command-processor.js:9980)
    at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/[email protected]/components/command-processor.js:12626)
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/[email protected]/components/command-processor.js:12643)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/k5/t81w4vh94rg1ps_h5tb_vbr00000gn/T/tmp0cz8wkgo/extensions/[email protected]/components/command-processor.js:623)

----------------------------------------------------------------------
Ran 1 test in 5.511s

so that seems to suggest that there is an error with the DOM tree, and the next element it tries to find (one with an id equal to 'id_list_table') cannot be found.

Why is this the case? How can I have Selenium test the addition of a list item and its existence in the table I've created?

Upvotes: 0

Views: 301

Answers (4)

Carl Brubaker
Carl Brubaker

Reputation: 1655

I ran into this problem too. I missed the name=item_text on the input of the home.html.

I'm not sure why I forgot that in the view, request.POST.get('item_text', '') looks for an input with that name.

Upvotes: 1

Maresh
Maresh

Reputation: 4712

I've seen this kind of problem before, and to work around it I usually first try a submit and then send a return key.

This is an extract from my custom wrapping class. elem would be your input element.

    try:
        self.debug("Trying regular submit")
        elem.submit()
    except:
        self.debug("Regular submit failed, sending RETURN key")
        elem.send_keys(Keys.RETURN)

Upvotes: 0

orange1
orange1

Reputation: 2939

Turns out that the selenium runserver command is unique to each app -- I was running the functional tests from the NEW (correct) repository, but I hadn't restarted my server with the NEW repository, I was using a server that was started using my OLD repository. Restarted the server and it now works as expected.

Upvotes: 2

cssko
cssko

Reputation: 3045

I don't think you want to use \n. If you want to send the enter key via selenium you would do:

 inputbox.send_keys('Buy peacock feathers')
 inputbox.send_keys(Keys.ENTER)

\n is a linefeed. This basically means it ends the current line. Check out this stackoverflow answer that explains in-depth.

Upvotes: 1

Related Questions