Using Selenium in Python to enter currency format text

一曲冷凌霜 提交于 2020-07-23 05:20:07

问题


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.

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);">

I have tried:

  • using the send_keys class just plain without any variation
  • clicking the box before using send_keys
  • clearing the box before using send_keys
  • waiting until the element can be located on the page and then doing all the above
  • using send_keys with Keys.NUMPAD#
  • adding $ in the beginning of the number
  • adding \$ in the beginning of the number
  • using Firefox driver instead of Chrome driver
  • entering the value as 100000.00 and 100000

Current version of my code:

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

I want it to enter 100000 in the box. Nothing appears in the box at all.


回答1:


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:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.moneying.input.currency.error#moneying"))).send_keys("$1000.0")
    
  • Using 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
    


来源:https://stackoverflow.com/questions/54120558/using-selenium-in-python-to-enter-currency-format-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!