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

时间:2015-11-12 16:11:37

标签: python html selenium

我正在创建一些测试用例,用于填充新的Web应用程序帐户。在这种情况下,我想一次添加几个用户。这就是我现在使用的:

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

...并继续这种模式,直到我达到了我想要的用户数量。 我知道python有一些我可以使用的while循环,我只是不知道在这种情况下如何实现它们。

1 个答案:

答案 0 :(得分:1)

# 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()