简体   繁体   English

Selenium Python - send_keys 时髦的行为

[英]Selenium Python - send_keys funky behavior

Using Selenium with PhantomJS (python 2.7) I find a text box on a page (Cisco Unity 7 FYI), and try to send keys.在 PhantomJS (python 2.7) 中使用 Selenium 我在页面上找到一个文本框(Cisco Unity 7 FYI),并尝试发送密钥。

driver.find_element_by_id("pwdPwdPassword").send_keys("12345678")
driver.save_screenshot('screen.png')

Looking at the screen shot, only 2 keys are actually sent.查看屏幕截图,实际上只发送了 2 个键。 Not sure which ones as Unity masks the password.不确定哪些是 Unity 掩码密码。 I notice that if I walk through my script in IDLE and slowly enter我注意到如果我在 IDLE 中浏览我的脚本并慢慢输入

driver.find_element_by_id("pwdPwdPassword").send_keys("1")
driver.find_element_by_id("pwdPwdPassword").send_keys("2")
driver.find_element_by_id("pwdPwdPassword").send_keys("3")
driver.find_element_by_id("pwdPwdPassword").send_keys("4")
....

The keystrokes actually make it into the text box.击键实际上进入了文本框。 I tried putting a time.sleep(2) between each send_keys in my script but the results are the same.我尝试在我的脚本中的每个 send_keys 之间放置一个 time.sleep(2) 但结果是一样的。 I am hoping there is a trick to this so I don't have to add 30 seconds to my script just to enter this password!我希望有一个技巧,所以我不必为了输入这个密码而在我的脚本中添加 30 秒!

Thanks!!!谢谢!!!

As an option, try to use jQuery:作为一种选择,尝试使用 jQuery:

def enter_password(driver, password):
    driver.execute_script("""$("#pwdPwdPassword").val('%s');"""" % password) 
    ## id must be unique per page

Maybe you should store the input field object like this:也许你应该像这样存储输入字段对象:

input_field = find_element_by_id("pwdPwdPassword")
input_field.send_keys("12345678")

Also try to update your PhantomJs webdriver if this doesn't work如果这不起作用,请尝试更新您的 PhantomJs 网络驱动程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM