Python 硒未设置输入字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28346240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:08:19  来源:igfitidea点击:

selenium not setting input field value

pythonseleniumselenium-webdriverautomation

提问by CracLock

Let's say we have this website https://www.coinichiwa.com/which has a BET AMOUNT input box. It's html is:

假设我们有这个网站https://www.coinichiwa.com/,它有一个 BET AMOUNT 输入框。它的 html 是:

<input autocomplete="off" id="betFa" name="a" maxlength="20" value="0.00000000" class="betinput" style="">

I need to add some value into it. Here is my code:

我需要为它添加一些价值。这是我的代码:

browser = webdriver.Firefox()
browser.get('https://www.coinichiwa.com')

browser.find_element_by_id("betFa").send_keys("0.00000005")
print browser.find_element_by_xpath("//input[contains(@id,'betFa')]").text

But it's neither setting it's value to "0.00000005" nor it printing the valueof input.

但它既没有将它的值设置为“0.00000005”,也没有打印value输入。

I'm not sure what's going wrong. Can you suggest? Why it's not working?

我不确定出了什么问题。你能建议吗?为什么它不起作用?

采纳答案by alecxe

You need to clear()the text input first:

您需要先clear()输入文本:

bet_fa = browser.find_element_by_id("betFa")
bet_fa.clear()
bet_fa.send_keys("0.00000005")

As for the your second problem - this is an inputand the value you enter into it is kept inside the valueattribute, not the text. Use get_attribute()method:

至于你的第二个问题 - 这是一个input,你输入的值保存在value属性中,而不是文本中。使用get_attribute()方法:

browser.find_element_by_xpath("//input[contains(@id,'betFa')]").get_attribute('value')