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

标签 python html selenium

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

    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 Selenium 添加多个用户的更有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676036/

相关文章:

python - 通过使用 Foo() 参数运行子命令,在主单击组命令上实例化 Foo() 类

html - 在 spring-mvc 中处理同名的多个 <input>

java - Firefox 更新后 Selenium WebDriver 未加载页面

Python - Selenium - 获取父类

python - pandas 中的排名是如何计算的

python - 印象笔记更新笔记资源

python - 在 python 中为列表中的项目生成词云

javascript - 为什么 Canvas 上的画线没有出现?

javascript - 有没有更快的方法来显示数组的每个索引

python - Selenium:在谷歌浏览器中上传文件