在Python中使用Selenium输入货币格式文本 [英] Using Selenium in Python to enter currency format text
问题描述
尝试在Python中使用Selenium将值"100000"输入到Web表单中,但是无论我尝试如何发送它,始终无法正常工作.
Trying to enter the value "100000" into a web form using Selenium in Python, but it is consistently not working no matter how I try to send it.
很抱歉,我对术语缺乏了解.我会尽力而为,但我是自学成才的新手.另外,我很抱歉,但是我不能告诉您该网站,否则我的雇主不会很高兴.
I apologize for my lack of knowledge about the terminology. I will try my best but I am self-taught and a novice. Also, I apologize but I cannot tell you the website or my employer would not be very happy.
网页上的框会自动填充一个美元符号.我知道网络表单中的框期望使用货币格式的整数. 当我查看网页上的html元素时,它提供了以下信息
The box on the webpage is automatically populated with a dollar sign. I know that the box in the web form is expecting an integer in currency format. When I look at the html element on the web page it gives the following information
<input type="number" step="1" name="moneying" size="35" id="moneying"
class="moneying input currency error" value="" data-type="currency" data-
mandopt="mand" required="" pattern="[\$]?[0-9]+[\.]?[0-9]*" min="500"
onblur="validate(this);">
我尝试过:
- 简单地使用send_keys类,没有任何变化
- 在使用send_keys之前单击框
- 在使用send_keys之前清除框
- 等待直到元素可以在页面上找到,然后执行以上所有操作
- 将send_keys与Keys.NUMPAD#一起使用
- 在数字开头添加$
- 在数字的开头加上\ $
- 使用Firefox驱动程序代替Chrome驱动程序
- 输入值100000.00和100000
我的代码的当前版本:
from selenium import webdriver
driver = webdriver.Chrome('location on my pc')
try:
driver.get(r"relevant web page")
moneying_box_wait = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.ID,"moneying")))
moneying_box = driver.find_element_by_id("moneying")
moneying_box.click()
moneying_box.clear()
moneying_box.send_keys("100000")
我希望它在框中输入100000.框中什么也没有出现.
I want it to enter 100000 in the box. Nothing appears in the box at all.
推荐答案
如果要发送字符序列而不是presence_of_element_located
,则需要使用element_to_be_clickable
,并且可以使用以下任一解决方案:
As you intend to send a character sequence instead of presence_of_element_located
you need to use element_to_be_clickable
and you can use either of the following solutions:
-
使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.moneying.input.currency.error#moneying"))).send_keys("$1000.0")
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='moneying input currency error' and @id='moneying']"))).send_keys("$1000.0")
注意:您必须添加以下导入:
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
这篇关于在Python中使用Selenium输入货币格式文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!