Selenium send_keys doesn't work if input type="number"

22,973

Solution 1

Because you are using Firefox 29. Please downgrade to Firefox 28, which is the one Selenium 2.41.0 supports to, see CHANGES file. Otherwise you need to wait for new Selenium updates.

Here is what I have tested working with Firefox 28:

from selenium import webdriver

DEMO_PAGE = '''
    data:text/html,
    <form><input type="number" id="field_id"></form>
'''

browser = webdriver.Firefox()
browser.get(DEMO_PAGE)

input_number = browser.find_element_by_id('field_id')
input_number.send_keys('12')

input_number_value = input_number.get_attribute('value')
print "input_number_value = " + input_number_value

See also: Selenium can't find fields with type number

Solution 2

I'm on Fedora (which doesn't provide old versions of packages like Firefox) so "downgrade Firefox" is a bit of a non-answer.

Luckily, an answer to a very similar question hints at a better solution -- setting the "dom.forms.number" Firefox preference to disable special treatment of input type="number". In Python:

profile = webdriver.FirefoxProfile()                                    
profile.set_preference("dom.forms.number", False)                       
browsers = webdriver.Firefox(profile)

Working with Firefox 29 and Selenium 2.41.0

Solution 3

I ran into this problem this morning. After upgrading Selenium, it now works properly.

So if you are reading this, run

pip install -U selenium

and try again. I went from Selenium version 2.41.0 to 2.42.1 and it now works properly with Firefox 30.0.

Share:
22,973
Nagasaki45
Author by

Nagasaki45

Musician and interaction designer and researcher. Explores Audio-Only Augmented Reality Systems in Interactive Social Context. My academic interests include interactive music consumption, Human Computer Interaction (HCI), Augmented Reality, Machine Learning and interactive art.

Updated on August 07, 2022

Comments

  • Nagasaki45
    Nagasaki45 almost 2 years

    I'm writing tests using selenium. In those tests I need to enter a number into a field in a form.

    Here is the html:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    
    <form>
        <input type="number" id="field_id">
    </form>
    
    
    </body>
    </html>
    

    And the code:

    browser = webdriver.Firefox()
    browser.get('file:///home/my_username/test.html')
    field = browser.find_element_by_id('field_id')
    field.send_keys('12')  # NOTHING HAPPEN!
    

    BTW, if I change the type of the field to "text" for example there is no problem at all. In addition, field.send_keys(Keys.UP) work great (but doesn't work when I'm using bootstrap) and field.clear() work all the time, as well as field.click().

    Selenium version: 2.41.0 Firefox version: 29.0