简体   繁体   English

Python Selenium 添加多个用户的更有效方法

[英]Python Selenium More Efficient Way of Adding Several Users

I am creating a few test cases that will populate a fresh web app account.我正在创建一些测试用例来填充一个新的 Web 应用程序帐户。 In this case I would like to add several users at once.在这种情况下,我想一次添加多个用户。 This is what I am using now:这就是我现在正在使用的:

    driver.find_element_by_css_selector("input.ss-med.small").clear()
    driver.find_element_by_css_selector("input.ss-med.small").send_keys("tester1")
    driver.find_element_by_xpath("(//input[@value=''])[2]").clear()
    driver.find_element_by_xpath("(//input[@value=''])[2]").send_keys("password")
    driver.find_element_by_css_selector("input.btn.btn-primary").click()

    driver.find_element_by_css_selector("input.ss-med.small").clear()
    driver.find_element_by_css_selector("input.ss-med.small").send_keys("tester2")
    driver.find_element_by_xpath("(//input[@value=''])[2]").clear()
    driver.find_element_by_xpath("(//input[@value=''])[2]").send_keys("password")
    driver.find_element_by_css_selector("input.btn.btn-primary").click()

    driver.find_element_by_css_selector("input.ss-med.small").clear()
    driver.find_element_by_css_selector("input.ss-med.small").send_keys("tester3")
    driver.find_element_by_xpath("(//input[@value=''])[2]").clear()
    driver.find_element_by_xpath("(//input[@value=''])[2]").send_keys("password")
    driver.find_element_by_css_selector("input.btn.btn-primary").click()

... and continuing this pattern until I have reached the number of users I desire. ...并继续这种模式,直到达到我想要的用户数量。 I know that python has some while loops that I could use, I just don't know how to implement them in this situation.我知道 python 有一些我可以使用的 while 循环,我只是不知道在这种情况下如何实现它们。

# if you want 100 users...
for n in range(100):
    # you should consider moving these to a separate object that represents the page
    # AKA Page Object Design
    username_field = driver.find_element_by_css_selector("input.ss-med.small")
    password_field = driver.find_element_by_xpath("(//input[@value=''])[2]")
    submit_button = driver.find_element_by_css_selector("input.btn.btn-primary")

    username_field.clear()
    username_field.send_keys('tester{}'.format(n))
    password_field.clear()
    password_field.send_keys('password')
    submit_button.click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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