简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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