How to send keys to an element with type = number using Selenium Python?
Asked Answered
I

3

3

I am trying to send a number to an element (a textbox) that only takes number values. This element is as follows:

Currently, I have this code (the code under this paragraph) to input a number into the element. Could anyone tell me why the number isn't showing up? Any help would be appreciated.

bpm = 121
inputBpm = driver.find_element(By.XPATH, "/html/body/mp-root/div/ ... /div[3]/div/input")
inputBpm.clear()
inputBpm.send_keys(str(bpm))

Edit: Here's a visual of what I'm trying to type into (after it's been cleared) The textbox

Here's the surrounding element:

<div _ngcontent-soo-c572="" class="track-bpm">
<label _ngcontent-soo-c572="" class="input-title">BPM</label>
<span _ngcontent-soo-c572="" class="input-title-info">(Beats per minute)</span>
<div _ngcontent-soo-c572="" class="input-text">
<input _ngcontent-soo-c572="" type="number" name="bpm" placeholder="0" min="0" max="999" step="0.01" class="ng-pristine ng-valid ng-touched"></div>
</div>
Imply answered 4/5, 2022 at 1:51 Comment(0)
A
1

You can simply do,

if inputBpm.get_attribute("type")  == "number":
    inputBpm.clear()
    inputBpm.send_keys(str(bpm))

Or you can just find that input tag with type = number using XPATH.

input_tag = driver.find_element(By.XPATH, "//input[contains(@type, 'number')]")
inputBpm.clear()
input_tag.send_keys("9023")
Average answered 4/5, 2022 at 2:21 Comment(2)
Neither of those snippets are working for some reason. It successfully clears the textbox, but it doesn't type anything. Here's the element again: <input _ngcontent-soo-c572="" type="number" name="bpm" placeholder="0" min="0" max="999" step="0.01" class="ng-pristine ng-valid ng-touched">Imply
@chucklenut40002 I tried them on here and they did work.Average
H
0

The <input> element is have the attributes type="number", min="0" and max="999"

<input _ngcontent-pxo-c572="" type="number" name="bpm" placeholder="0" min="0" max="999" step="0.01" class="ng-valid ng-touched ng-dirty">

Solution

To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    bpm = 121
    my_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-valid.ng-touched.ng-dirty[name='bpm']")))
    my_element.click()
    my_element.clear()
    my_element.send_keys(str(bpm))
    
  • Using XPATH:

    bpm = 121
    my_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-valid ng-touched ng-dirty' and @name='bpm']")))
    my_element.click()
    my_element.clear()
    my_element.send_keys(str(bpm))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Hirundine answered 4/5, 2022 at 12:32 Comment(1)
When I do either of these snippets I get a timeout error. Could I be implementing them wrong?Imply
A
0

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

Arresting answered 18/3 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.