Python。硒。如何等待新窗口打开?

2024-05-15 09:04:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个twitter按钮,点击后新窗口打开,但在打开前有一个timut在几秒钟内,所以有办法等待吗?我现在的处境很糟。。。

    Ui.click_el(link.W9FormNodes.TWITTER_BUTTON)
    # Wait for new window and switch to it
    time.sleep(3)
    aw = driver.window_handles
    driver.switch_to_window(aw[1])

所以我需要更好的东西,那只需要等那么多秒。

我想我是这样解决的。但也许有人会评论如何做得更好。

    aw = Ui.click_and_wait_for_window(link.W9FormNodes.TWITTER_BUTTON)
    driver.switch_to_window(aw[1])

下面是一个点击按钮等待新窗口的方法:

    def click_and_wait_for_window(self, node):
        current_value = old_value = self.driver.window_handles
        self.click_el(node)
        while len(current_value) == len(old_value):
            time.sleep(0.05)
            current_value = self.driver.window_handles
        return current_value

Tags: andtoselfuiforvaluedrivercurrent