如何使用Selenium Python将键发送给具有类型=数字的元素?

发布于 2025-01-26 16:25:23 字数 952 浏览 6 评论 0 原文

我正在尝试将一个数字发送到仅采用数字值的元素(文本框)。此元素如下:

当前,我有此代码(本段下的代码)将一个数字输入到该元素中。谁能告诉我为什么没有出现这个数字?任何帮助将不胜感激。

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

编辑: 这是我想输入的内容的视觉效果(清除后)

这是周围的元素:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2025-02-02 16:25:23

您可以简单地做,

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

也可以使用xPath的 type = number 找到输入标签。

input_tag = driver.find_element(By.XPATH, "//input[contains(@type, 'number')]")
inputBpm.clear()
input_tag.send_keys("9023")

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")
顾北清歌寒 2025-02-02 16:25:23

&lt; input&gt; 元素具有属性 type =“ number” min =“ 0” 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">

解决方案

,以将字符序列发送到元素需要诱导 webdriverwait 对于 element_to_be_clickable() 您可以使用以下任何一个 >

  • 使用 css_selector

      bpm = 121
    my_element = webdriverwait(驱动程序,20).ultil(ec.element_to_be_clickable(((by.css_selector
    my_element.click()
    my_element.clear()
    my_element.send_keys(str(bpm))
     
  • 使用 xpath

      bpm = 121
    my_element = webdriverwait(驱动程序,20).ultil(ec.element_to_be_be_clickable(((by.xpath,“ // input [ @class ='ng-valid ng-valid ng-touched ng-dirty'and @name ='bpm'bpm') )
    my_element.click()
    my_element.clear()
    my_element.send_keys(str(bpm))
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

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
    
×眷恋的温暖 2025-02-02 16:25:23

就我而言,我正在研究Chromedriver。我的表单具有输入类型=数字。尝试发送_keys时,什么也不会发生。

我尝试了 driver.execute_script(f“ gragments [0] .value ='{text}';“,element)
该解决方案写入数字,但表格未识别。

它与:

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

我还尝试了另一种 pyautogui.typewrite(text),但它与以前的解决方案产生相同的结果。

因此,我需要执行真正的键盘触摸,而不是由Selenium进行的模拟。

我很遗憾地说,必须安装2个库:
pynput (需要 evdev
最初,PIP安装不起作用,因此我必须执行以下命令:

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

old 解决方案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)

此解决方案在无尽模式下不起作用。

编辑更好的解决方案
硒能够与钥匙相互作用。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文