【问题标题】:Why does this code need to call WebDriverWait twice to work?python 需要调用 WebDriverWait 两次
【发布时间】: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


【解决方案1】:

它看起来绝对是多余的。
您可以删除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()

不确定身份,从您的问题中复制粘贴...

【讨论】:

  • 我以头部模式运行它,所以我可以看到网络浏览器。浏览器什么也没做,只调用了 wait until.element_to_be_clickable 两次。我碰巧找到了这个页面:stackoverflow.com/questions/31792301/… 其中提到了方法:elm = wait.until(EC.presence_of_element_located(( 所以我用 wait.until(EC.presence_of_element_located((By.ID, self.passwordId)) 替换了 element_to_be_clickable ) 现在它似乎起作用了。我会等到明天。通常第二天早上事情就不再起作用了。:)
  • 好吧,绝对不是所有的元素都是可点击或可见的。但是,如果它是您要单击的元素,则它必须是可单击的。等待!可见屏幕内的该元素是否未被其他元素覆盖?也许您必须滚动到它才能将其带入视图?...您可以分享您正在处理的网页吗?
猜你喜欢
  • 1970-01-01
  • 2018-07-02
  • 2021-04-28
  • 2011-04-19
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多