简体   繁体   English

python 需要调用 WebDriverWait 两次

[英]python Need to call WebDriverWait twice

I am writing an automated test with Selenium in Python.我正在用 Python 用 Selenium 编写一个自动化测试。 I do not understand why in the piece of code below the until method of WebdriverWait needs to be called twice, because otherwise the text input will not be filled.不明白为什么下面这段代码中WebdriverWait的until方法需要调用两次,否则文本输入不会被填充。 What do I need to add or delete so that the html input field is filled without the need to call to WebDriverWait.until() twice?我需要添加或删除什么,以便在不需要两次调用 WebDriverWait.until() 的情况下填充 html 输入字段? The method looks like this: 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)该方法如下所示: 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) 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()

It looks absolutely redundant.它看起来绝对多余。
You can remove one of element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId))) lines and the sleep(2) too.您也可以删除element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))行和sleep(2)
Possibly your site loads too much time and the 10 seconds timeout is not enough.可能您的网站加载时间过长而 10 秒超时是不够的。
In this case you can set the timeout to 30 or even 60 seconds, as following:在这种情况下,您可以将超时设置为 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()

Not sure about identation, copy-pasted it from your question...不确定身份,从您的问题中复制粘贴...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM