Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Sarah92 Avatar asked Jan 28 '13 14:01

Sarah92


People also ask

How do I use Selenium Python in Firefox?

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript. Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.

Can I use Selenium with Firefox?

Mozilla Firefox is one of the most widely used browsers in the world. It has enhanced features and is supported by many of the latest testing tools and techniques. One such tool is Selenium which uses Firefox WebDriver to link the test cases with the Firefox browser.

How many ways you can do sendKeys () in Selenium?

Now, as we discussed, Selenium WebDriver provides two ways to send any keyboard event to a web element: sendKeys() method of WebElement class.


2 Answers

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')
like image 156
Corey Goldberg Avatar answered Oct 12 '22 21:10

Corey Goldberg


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)

like image 34
Arun Avatar answered Oct 12 '22 21:10

Arun