我正在 Python 中使用 Selenium 编写自动化测试。不明白为什么下面这段代码中WebdriverWait的until方法需要调用两次,否则文本输入不会被填充。我需要添加或删除什么,以便在不需要两次调用 WebDriverWait.until() 的情况下填充 html 输入字段?该方法如下所示:
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()
答案 0 :(得分:0)
看起来完全多余。
您也可以删除 element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))
行之一和 sleep(2)
。
可能是您的网站加载时间过长而 10 秒的超时时间不够。
在这种情况下,您可以将超时设置为 30 甚至 60 秒,如下所示:
class SomeClass:
usernameId = 'username'
passwordId = 'password'
def doSomething(self, username, password):
wait = WebDriverWait(self.driver, 60)
# 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)))
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)
inloggenButton = wait.until(EC.element_to_be_clickable((By.ID, self.inloggenButtonId)))
inloggenButton.click()
GeneralPage(self.driver).logged_on()
不确定身份,从您的问题中复制粘贴...