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

7

10

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

Traveled answered 1/5, 2014 at 17:22 Comment(3)
What if you call send_keys in a loop: for x in '12': field.send_keys(x)?Vassal
What happens if you click the field first? Also, can you manually enter values in the field?Lamaism
@RobbieWareham Clicking the field manually doesn't help. field.click() does focus on the input field. field.send_keys also focus on the field but nothing more.Traveled
I
6

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

Improvvisatore answered 1/5, 2014 at 21:12 Comment(4)
With multiple versions of firefox installed, how would I tell selenium to use a specific one?Washburn
Is there a Selenium bug report for this? Can you provide a link?Chamorro
Also, it still doesn't seem to work even with Firefox 31 (tested on Sauce Labs with Linux configured).Chamorro
I created an issue for this in the Selenium tracker here: code.google.com/p/selenium/issues/detail?id=7809Chamorro
R
5

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

Resort answered 27/5, 2014 at 12:53 Comment(0)
S
2

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.

Stedman answered 22/7, 2014 at 8:57 Comment(0)
B
0

You can probably use Javascript to tackle this issue. 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 using Javascript solved it.

Bolometer answered 12/3, 2015 at 0:51 Comment(0)
S
0

In my case selenium Send_keys work fine in 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`

it is the web https://pypi.python.org/pypi/selenium

Spalla answered 18/5, 2016 at 7:57 Comment(0)
J
0

I resolved this issue in this way:

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)
Jurgen answered 24/5, 2016 at 16:39 Comment(0)
H
0

Duplicate of https://mcmap.net/q/1164659/-how-to-send-keys-to-an-element-with-type-number-using-selenium-python

In my case, I'm working on chromedriver. I have a form with an input type = number. When attempting to send_keys, nothing happens.

I tried driver.execute_script(f"arguments[0].value = '{text}';", element). This solution writes the number BUT the form does not recognize it.

It's exactly the same as :

script = f"""document.evaluate({xpath}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = '{text}';"""
driver.execute_script(script)

I also tried an alternative pyautogui.typewrite(text) but it produces the same result as the previous solution.

So I need to perform a real keyboard touch instead of the simulated one by selenium.

I'm sorry to say that it is mandatory to install 2 libraries : pynput (which requires evdev) Initially, the pip install did not work, so I had to execute the following commands:

sudo apt-get install build-essential
pip install evdev
pip install pynput

OLD SOLUTION TLDR:

    from pynput.keyboard import Key, Controller
    text = "my text"
    xpath = "//my//xpath"
    self.wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
    keyboard = Controller()
    for char in text:
        keyboard.press(char)
        keyboard.release(char)

This solution doesn't works in an endless mode.

EDIT BETTER SOLUTION Selenium is capable of interacting with keys.

def inputNumberWithKeyboard(self, element, number):

  for digit in number:
      self.actions.click(element).send_keys(eval(f"Keys.NUMPAD{digit}")).perform()
      # if digit is 1 the command will be
      # self.actions.click(element).send_keys(eval(Keys.NUMPAD1).perform()

Halves answered 18/3 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.