3

I am currently programming the examples in "Test-Driven Development with Python", more specifically the first functional test. But for some weird reason, send_keys does not work properly. This is what I'm trying right now - and I changed the implicit wait for explicit waits, by the way!

    inputbox = self.browser.find_element_by_id('id_new_item')
    self.assertEqual( # This passes, it's here just for completeness
        inputbox.get_attribute('placeholder'),
        'Enter a To-Do item'
    )
    inputbox.send_keys('Buy peacock feathers')
    inputbox.send_keys(Keys.ENTER) # Everything okay up to here
    WebDriverWait(self.browser, 10).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, "table#id_list_table tr td"), "Buy peacock feathers")
    )
    table = self.browser.find_element_by_id('id_list_table')

    rows = table.find_elements_by_tag_name('tr')        
    self.assertIn('1: Buy peacock feathers', [row.text for row in rows])

    inputbox1 = self.browser.find_element_by_id('id_new_item') # Changed the variable only to test if it would hang too - and it does
    inputbox1.send_keys('Use peacock feathers to make a fly')
    inputbox1.send_keys(Keys.ENTER) # This hangs
    self.fail()
    WebDriverWait(self.browser, 10).until(
        EC.text_to_be_present_in_element((By.CSS_SELECTOR, "table#id_list_table tr td"), "Use peacock feathers to make a fly")
    )

It never reaches self.fail(). I tried moving it to the previous line, and the test fails, as it should. But inputbox1.send_keys(Keys.ENTER) never works, and when I see the browser as the test runs, inputbox1.send_keys('Use peacock feathers to make a fly') never writes "Use peacock feathers to make a fly" in the input box.

What is happening? I am using the latest Selenium (I think, I downloaded it a couple days ago just checked, I do have the latest version), Python and Django versions, and this opens Firefox Developer Edition in my laptop. Thank you.

EDIT: I tried disabling multi-process in Firefox, but the outcome does not change - it still hangs when trying to write and press enter.

7
  • How about inputbox1.send_keys(Keys.RETURN)? Commented Feb 16, 2016 at 5:25
  • Also, what selenium and firefox versions are you using? Commented Feb 16, 2016 at 5:26
  • Just did that, same output. Firefox Developer Edition is version 46.0a2, and Selenium is 2.52.0. Commented Feb 16, 2016 at 5:31
  • Okay, I think it might be that the firefox is too new for this selenium version. Try downgrading firefox to the latest stable. Let me know, thanks. Commented Feb 16, 2016 at 5:32
  • 1
    You may also try with chrome so that we can say for sure this is firefox-specific. Commented Feb 16, 2016 at 5:38

2 Answers 2

1

Thanks to alexce for helping me!

I changed the following in my test class:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

def setUp(self):
    binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
    self.browser = webdriver.Firefox(firefox_binary=binary)

The issue? I was using Firefox Developer Edition, which apparently is not supported by Selenium entirely. So I just forced Selenium to load my regular Firefox and it does not hang anymore!

Sign up to request clarification or add additional context in comments.

Comments

1

Oddly enough I couldn't get anything to run in my Ubuntu shell but it would run via IPython from Jupyter Notebook on the exact same server.

I had to add a virtual display into the code to make it run from the shell as a .py script...

If it helps anyone facing the similiar problem here are the lines of code I added to my script and the send keys start to work without an issue. Also seems that even if I leave the headless switch on for my chrome driver it is still needed.

from pyvirtualdisplay import Display

# Set screen resolution to 1366 x 768. This is needed 
# to run in the shell. Seems fine in iPython.
display = Display(visible=0, size=(1366, 768))
display.start()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.