Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

python - Selenium send_keys doesn't work if input type="number"

I'm writing tests using selenium. In those tests I need to enter a number into a field in a form.

Here is the html:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form>
    <input type="number" id="field_id">
</form>


</body>
</html>

And the code:

browser = webdriver.Firefox()
browser.get('file:///home/my_username/test.html')
field = browser.find_element_by_id('field_id')
field.send_keys('12')  # NOTHING HAPPEN!

BTW, if I change the type of the field to "text" for example there is no problem at all. In addition, field.send_keys(Keys.UP) work great (but doesn't work when I'm using bootstrap) and field.clear() work all the time, as well as field.click().

Selenium version: 2.41.0 Firefox version: 29.0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm on Fedora (which doesn't provide old versions of packages like Firefox) so "downgrade Firefox" is a bit of a non-answer.

Luckily, an answer to a very similar question hints at a better solution -- setting the "dom.forms.number" Firefox preference to disable special treatment of input type="number". In Python:

profile = webdriver.FirefoxProfile()                                    
profile.set_preference("dom.forms.number", False)                       
browsers = webdriver.Firefox(profile)

Working with Firefox 29 and Selenium 2.41.0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...