使用页面对象模型在 Python Selenium 中显式等待 [英] Explicit wait in Python Selenium with page object model

查看:58
本文介绍了使用页面对象模型在 Python Selenium 中显式等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的显式等待不是等到元素出现.它实际上等待了我声明的秒数,然后测试仍然失败.如果我在完全相同的地方放置一个隐式等待测试通过.从我正在阅读的内容来看,最好的做法是尽可能避免隐式等待.难道我做错了什么?

My explicit wait isn't waiting until the element is present. It literally waits the amount of seconds I declared and then the tests still fails. If I place a implicit wait in the exact same place the test passes. From what I'm reading, it's best practise to avoid implicit waits as much as possible. Am I doing something wrong?

我在 base_page 中创建了一个方法,如下所示:

I have made a method in the base_page like so:

def _wait_for_is_displayed(self, locator, timeout):
        try:
            wait = WebDriverWait(self.driver, timeout)
            wait.until(expected_conditions.visibility_of_element_located((locator["by"], locator["value"])))
        except TimeoutException:
            return False
        return True

然后我像这样在页面对象中调用 _wait_for_is_displayed 方法,但失败了:

then i call the _wait_for_is_displayed method in a page object like so, but fails:

 def relatie_page_present(self):
     self._wait_for_is_displayed(self._open_actieve_polissen_tab, 10)

 def relatie_page_(self):
     self._click(self._open_relatie_details)
     self.relatie_page_present()

我得到的错误是:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"td.first > a"}

这通过了:

 def relatie_page_present(self):
        self.driver.implicitly_wait(10)

def relatie_page_(self):
    self._click(self._open_relatie_details)
    self.relatie_page_present()

最后在我的测试套件中,我调用了 relatie_page_present 和 relatie_page_ 方法.

Lastly in my test suite i call the relatie_page_present and relatie_page_ methods.

推荐答案

为了阐明隐式等待的工作原理,它在驱动程序上设置了一次,然后在驱动程序实例的生命周期内应用.所以调用 self.driver.implicitly_wait(10) 实际上并没有在那个时刻等待 ...它只是设置驱动程序从那个时候开始等待一个元素位于.我认为这让很多人感到困惑,因为我看到了很多.

To clarify how implicit wait works, it is set one time on the driver and then is applied for the life of the driver instance. So calling self.driver.implicitly_wait(10) doesn't actually wait at that moment... it just sets the driver to wait from that point on when an element is located. I think this confuses a lot of people because I see it quite a bit.

无论如何,如果我是你,我会使用像下面这样的函数,等待元素可点击,然后点击它.您可以在需要等待元素被点击的任何时候调用它.

Anyway, if I were you, I would use a function like the below that waits for the element to be clickable and then clicks it. You can call it any time you need to potentially wait for an element to be clicked.

def _wait_and_click(self, locator, timeout):
    try:
        wait = WebDriverWait(self.driver, timeout)
        wait.until(expected_conditions.element_to_be_clickable((locator["by"], locator["value"]))).click()
    except TimeoutException:
        return False
    return True

这篇关于使用页面对象模型在 Python Selenium 中显式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆