如何使用Selenium Python将键发送给具有类型=数字的元素?
我正在尝试将一个数字发送到仅采用数字值的元素(文本框)。此元素如下:
当前,我有此代码(本段下的代码)将一个数字输入到该元素中。谁能告诉我为什么没有出现这个数字?任何帮助将不胜感激。
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地做,
也可以使用xPath的
type = number
找到输入标签。You can simply do,
Or you can just find that input tag with
type = number
using XPATH.&lt; input&gt;
元素具有属性type =“ number”
,min =“ 0”
和max =“ 999”
解决方案
,以将字符序列发送到元素需要诱导 webdriverwait 对于 element_to_be_clickable() 您可以使用以下任何一个 > :
使用 css_selector :
使用 xpath :
注意:您必须添加以下导入:
The
<input>
element is have the attributestype="number"
,min="0"
andmax="999"
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:
Using XPATH:
Note: You have to add the following imports :
就我而言,我正在研究Chromedriver。我的表单具有
输入类型=数字
。尝试发送_keys时,什么也不会发生。我尝试了
driver.execute_script(f“ gragments [0] .value ='{text}';“,element)
。该解决方案写入数字,但表格未识别。
它与:
我还尝试了另一种
pyautogui.typewrite(text)
,但它与以前的解决方案产生相同的结果。因此,我需要执行真正的键盘触摸,而不是由Selenium进行的模拟。
我很遗憾地说,必须安装2个库:
pynput
(需要evdev
)最初,PIP安装不起作用,因此我必须执行以下命令:
old 解决方案tldr:
此解决方案在无尽模式下不起作用。
编辑更好的解决方案
硒能够与钥匙相互作用。
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 :
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 requiresevdev
)Initially, the pip install did not work, so I had to execute the following commands:
OLD SOLUTION TLDR:
This solution doesn't works in an endless mode.
EDIT BETTER SOLUTION
Selenium is capable of interacting with keys.