pythonseleniumwebdriverwaitexpected-conditionconstraint-validation-api

How to enter numeric value in input field using Python Selenium?


I've got a script writing values into a web page, and all values write except for one field that keeps throwing up the following error: enter image description here (Screenshot provided b/c in other similar questions many comments said this is impossible to happen on a web page.) "Please enter a numeric value."

Here's my code:

workcenter_to_add = {}
workcenter_to_add['BatchCycle'] = str(2.78)
# driver = my_chrome_webpage
WebDriverWait(driver, wait_time).until(EC.presence_of_element_located((By.XPATH, "//input[@id='BatchSize']"))).send_keys(workcenter_to_add['BatchCycle'])

As everyone knows, if I do not input the 2.78 value in as a string WebDriver throws an error. But my page demands a numeric value. I'm stuck.

I've Googled around and not found a usable answer to this. It seems if you're using Java there's a setAttribute method you can use, but if you're using Pythonyou've got to figure something out.

For example, the question here looked promising but I could not find the String or how to import it to get it to work. There's a couple of other much older questions that talk about executing java but I have had no luck getting them to work.

I've got the page-source HTML here: https://drive.google.com/open?id=1xRNPfc5E65dbif_44BQ_z_4fMYVJNPcs


Solution

  • I'm sure this is going to be an unpopular answer, but this is how I got it work.

    The field in this question and another field on another page in the same ERP system were throwing the same error. send_keys() would not work no matter what I tried.

    That's when I put on my thinking cap and starting trying other ways.

    I tried entering the information into another field on the page that would accept numbers via send_keys() and then cutting and pasting the values into the field that would not accept the value had I used send_keys(). It worked!

    Here's a code snippet I used on the different page with the same issue:

                        elem1 = driver.find_element_by_id('txtNote')
                        elem1.send_keys(rm['txtGross_Weight'])
                        elem1.send_keys(Keys.CONTROL, 'a') #highlight all in box
                        elem1.send_keys(Keys.CONTROL, 'x') #cut
                        elem2 = driver.find_element_by_id('txtGross_Weight')
                        elem2.send_keys(Keys.CONTROL, 'v') #paste
    

    I was looking for a high tech answer when a low tech work around sufficed.

    Is it code or methodology I'd write on a job resume? Probably not. Did it work and can I live with it? Yes. Thank you for the people who tried to answer.