selenium webdriver sendkeys() 使用 python 和 firefox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14564539/
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-18 11:50:22  来源:igfitidea点击:

selenium webdriver sendkeys() using python and firefox

pythonfirefoxseleniumselenium-webdriver

提问by Sarah92

I am using selenium 2.25.0, firefox 3and python 2.6.6. I am trying to run a selenium function which uses sendkeys():

我正在使用selenium 2.25.0,firefox 3python 2.6.6. 我正在尝试运行一个硒函数,它使用sendkeys()

 Webdriver.find_element_by_name( 'j_username' ).clear()
 webdriver.find_element_by_name( 'j_username' ).send_keys( "username" )

This code works running from my machine. However running from another machine the username field gets left empty and continues with the rest of the script(without reporting any errors).

这段代码可以在我的机器上运行。然而,从另一台机器运行时,用户名字段被留空并继续执行脚本的其余部分(不报告任何错误)。

I can see that the field is cleared before sending the username is attempted so I know there is not a problem with finding the button/naming of the button. I've tried putting pauses inbetween clearing the field and sending the username but this also doesn't seem to work.

我可以看到在尝试发送用户名之前该字段已被清除,因此我知道查找按钮/按钮命名没有问题。我试过在清除字段和发送用户名之间暂停,但这似乎也不起作用。

I need to keep my firefox and selenium versions the same, is there anything else I can look at to solve this problem?

我需要保持我的 firefox 和 selenium 版本相同,还有什么可以解决这个问题的吗?

回答by Corey Goldberg

your code looks odd. typically, you locate an element, and then do actions with it... rather than locating it each time.

你的代码看起来很奇怪。通常,您定位一个元素,然后对其进行操作……而不是每次都定位它。

try something like this:

尝试这样的事情:

from selenium import webdriver

driver = webdriver.Firefox()
elem = driver.find_element_by_name('j_username')
elem.clear()
elem.send_keys('username')

回答by Arun

Use following as a work around I think It may work.

使用以下作为一种解决方法,我认为它可能有效。

driver = webdriver.Firefox()
elem = driver.find_element_by_name('j_username')
elem.clear()
app = Application.Application()
app.window_(title_re='*.Firefox.*').TypeKeys('username')

Last two lines are in Python(pyWinauto)

最后两行在 Python(pyWinauto) 中

回答by vulpineblazeyt

My problem was identical, and I solved it by going from selenium==2.42.1 down to selenium==2.25.0

我的问题是相同的,我通过从 selenium==2.42.1 降到 selenium==2.25.0 解决了它

After changing my selenium version the test was able to send_keys() and submit the form using send_keys(Key.ENTER)

更改我的硒版本后,测试能够使用 send_keys() 并使用 send_keys(Key.ENTER) 提交表单

I am currently running windowless on a remote Debian Squeeze 6.0.8 server with Iceweasel 3.5.16

我目前在带有 Iceweasel 3.5.16 的远程 Debian Squeeze 6.0.8 服务器上运行无窗口

Mozilla Iceweasel 3.5.16, Copyright (c) 1998 - 2010 mozilla.org

Distributor ID: Debian
Description:    Debian GNU/Linux 6.0.8 (squeeze)
Release:        6.0.8
Codename:       squeeze