有没有办法使用 Python Selenium 在不同的文本框中自动输入文本?

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

我想测试一个包含很多文本框的注册表单。
而不是手动对每个文本框使用 .sendkeys(),有没有一种自动将文本输入页面中每个文本框的方法?

您可以使用页面工厂来存储定位器并每次都从那里使用,或者您也可以尝试 Data/Text 驱动框架。

如果我对你的问题的理解正确,你希望文本框的代码行尽可能少,每个文本框都有各自的 xpaths 吗?

执行此操作的一种方法是使用 xpath(和文本)列表,您可以遍历并自动填充该列表。

请注意,您可能希望添加 WebdriverWait 以等待元素加载到屏幕上,但此代码段可能会帮助您。

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