简体   繁体   中英

Is there a way to automatically input text in the different text box using Python Selenium?

I wanted to test a registration form with a lot of text boxes.
Instead of manually using the .sendkeys() to each text box, is there a way to automatically input texts into each and every textbox in the page?

You may use page factory to store the locators and use from there every time or you can try the Data/Text driven framework also.

If I understand your question correctly, you'd like to have as little lines of code as possible for a form of text boxes with each their individual xpaths?

A way to do this you could use a list of xpaths (and text), which you iterate through and fill in automatically.

Note, you'd probably want to add a WebdriverWait to wait for the element to be loaded on the screen but this snippet might help you on your way.

# list of tuples consisting of a xpath and text to fill in that textbox
my_list = [('//*[@id="textbox_email"]', 'example@email.com'), ('//*[@id="textbox_password"]', 'pwd123'), (..., ...)]

for xpath, text in my_list:
    driver.find_element_by_xpath(xpath).send_keys(text)

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