Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate text input by name using Selenium WebDriver?

I'm a selenium newbie and just trying to learn the basics. I have a simple CherryPy webapp that takes a first name and last name as input:

My Webapp:

<p>
    <label></label>
    <input name="first_name"></input>
    <br></br>
</p>
<p>
    <label></label>
    <input name="last_name"></input>
    <br></br>
</p>

In my python console I have:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://localhost:8080')

The page loads fine in FF but I'm a little lost on how to get text into the 'first_name' and 'last_name' text boxes. I see examples where you do something like inputElement = driver.find_element_by_id("n") and then inputElement.send_keys('my_first_name') but I don't have an id...just a name. Do I need to add stuff to my web page? Thanks!

like image 501
darksideofthesun Avatar asked Feb 10 '23 14:02

darksideofthesun


1 Answers

You can use find_element_by_name:

driver.find_element_by_name('first_name').send_keys("my_first_name")
driver.find_element_by_name('last_name').send_keys("my_last_name")
like image 64
zero323 Avatar answered Feb 12 '23 03:02

zero323