Selenium send_keys does not work if input type = "number"

I am writing tests using selenium. In these tests, I need to enter a number in the form field.

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! 

By the way, if I change the type of the field to "text", for example, there is no problem at all. Also, field.send_keys(Keys.UP) works fine (but doesn't work when I use bootstrap) and field.clear() works all the time, as well as field.click() .

Selenium version: 2.41.0 Firefox version: 29.0

+8
python firefox selenium selenium-webdriver
source share
6 answers

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

Here is what I tested while 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 cannot find fields with type number

+5
source share

I am in Fedora (which does not provide older versions of packages such as Firefox), so the "downgrade Firefox" is a bit unresponsive.

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

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

Work with Firefox 29 and Selenium 2.41.0

+4
source share

I ran into this problem this morning. After updating Selenium, it now works correctly.

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 now it works correctly with Firefox 30.0.

+2
source share

Perhaps you can use Javascript to solve this problem. The following code is in Java, but it can probably be done similarly in Python:

 ((IJavaScriptExecutor)webdriver) .ExecuteScript("document.getElementById('field_id').value='12';"); 

I had the same issue and I used Javascript.

0
source share

In my case, selenium Send_keys work fine this way.

 from selenium import webdriver from selenium.webdriver.common.keys import Keys browser = webdriver.Firefox() browser.get('http://www.yahoo.com') assert 'Yahoo' in browser.title elem = browser.find_element_by_name('p') # Find the search box elem.send_keys('seleniumhq' + Keys.RETURN) browser.quit()enter code here` 

this is web https://pypi.python.org/pypi/selenium

0
source share

I solved this problem as follows:

 locator = <element xpath> field = browser.find_element_by_xpath(to_unicode(**locator**,"utf-8")) if(field != None): field.send_keys(Keys.CONTROL + 'a') field.send_keys(value) 
0
source share

All Articles