python selenium: does not wait until page is loaded after a click() command

前端 未结 2 1969
忘掉有多难
忘掉有多难 2021-02-05 07:21

does someone know how to wait until the page is loaded? I tried all possible variants I found on the web but is simply does not work.

I need to wait after I trigger a cl

2条回答
  •  野的像风
    2021-02-05 07:45

    Fundamentally if the page is being generated by JavaScript then there is no way to tell when it has "finished" loading.

    However. I typically try to retrieve a page element, catch the exception and keep on trying until a timeout is reached. I might also check that the element is visible not merely present.

    There must be some DOM element whose visibility you can use to test that a page has "finished" loading. I typically have a wait_for_element_visibility, and wait_for_element_presence functions in my test cases.

    def wait_for_visibility(self, selector, timeout_seconds=10):
        retries = timeout_seconds
        while retries:
            try:
                element = self.get_via_css(selector)
                if element.is_displayed():
                    return element
            except (exceptions.NoSuchElementException,
                    exceptions.StaleElementReferenceException):
                if retries <= 0:
                    raise
                else:
                    pass
    
            retries = retries - 1
            time.sleep(pause_interval)
        raise exceptions.ElementNotVisibleException(
            "Element %s not visible despite waiting for %s seconds" % (
                selector, timeout_seconds)
        )
    

    get_via_css is one of my own functions but I hope it's fairly clear what it is doing.

提交回复
热议问题