如何使用Python Selenium在输入字段中输入数值? [英] How to enter numeric value in input field using Python Selenium?

查看:901
本文介绍了如何使用Python Selenium在输入字段中输入数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本将值写入网页,并且所有值都写入,除了一个字段不断抛出以下错误: (屏幕快照提供了b/c的其他类似问题,许多评论说这不可能在网页上发生.) 请输入一个数字值."

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: (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."

这是我的代码:

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'])

众所周知,如果我不输入2.78值,因为string WebDriver会引发错误.但是我的页面需要一个数值.我被卡住了.

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.

我已经在Google周围搜索了,但没有找到可用的答案.看来,如果您使用的是Java,则可以使用setAttribute的方法,但是如果您使用的是Python,则必须弄清楚一些事情.

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.

例如,问题此处很有希望,但我找不到String或如何导入它以使其正常工作.还有其他一些关于执行java的更老的问题,但是我没有运气让它们工作.

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.

我在这里有页面源HTML: https://drive.google.com/open?id=1xRNPfc5E65dbif_44BQ_z_4fMYVJNPcs

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

推荐答案

我确定这将是一个不受欢迎的答案,但这就是我的工作方式.

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

此问题中的字段和同一ERP系统中另一页上的另一个字段都引发了相同的错误. send_keys()不管我尝试什么都行不通.

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.

我尝试将信息输入页面上的另一个字段,该字段将通过send_keys()接受数字,然后将值剪切并粘贴到如果我使用send_keys()则不接受该值的字段中.奏效了!

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.

这篇关于如何使用Python Selenium在输入字段中输入数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆