【发布时间】:2021-09-24 10:29:01
【问题描述】:
我正在用 Python 中的 Selenium 编写一个自动化测试。我不明白为什么下面这段代码中WebdriverWait的until方法需要调用两次,否则文本输入将不会被填充。我需要添加或删除什么以便填充 html 输入字段而无需调用 WebDriverWait.until() 两次?该方法如下所示:
class SomeClass:
usernameId = 'username'
passwordId = 'password'
def doSomething(self, username, password):
wait = WebDriverWait(self.driver, 10)
sleep(2)
# TODO: find out why one wait is not enough, and how it should be done properly.
element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))
# Why is the seconde line needed? Without it the input is not filled. It should not be?
element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))
element.send_keys(username)
d = self.driver
wait.until(lambda d: element.get_attribute("value") == username)
pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
# Why is the seconde line needed? Without it the input is not filled. It should not be?
pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
pwdElement.send_keys(password)
wait.until(lambda d: pwdElement.get_attribute("value") == password)
sleep(2)
inloggenButton = wait.until(EC.element_to_be_clickable((By.ID, self.inloggenButtonId)))
inloggenButton.click()
GeneralPage(self.driver).logged_on()
【问题讨论】:
-
那么,问题是什么?
-
如果第二行: element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId))) 省略,文本输入不填充变量username中包含的文本.我需要添加或删除什么才能在不第二次调用 WebDriverWait.until() 的情况下填充输入?
-
等待 = WebDriverWait(self.driver, 10)
-
我明白了。但是你为什么破坏了编辑后的帖子?
-
你说得对,我的问题定义有点模糊。感谢您的反应!
标签: python selenium webdriverwait html-input-element