Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

selenium webdriver sendkeys() using python and firefox

I am using selenium 2.25.0, firefox 3 and python 2.6.6. I am trying to run a selenium function which uses 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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...