我正在创建一些测试用例,这些用例将填充一个新的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循环可以使用,我只是不知道如何在这种情况下实现它们。
最佳答案
# 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()
关于python - Python Selenium添加多个用户的更有效方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33676036/