简体   繁体   中英

Selenium send_keys hangs on Python

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.

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!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM