python3selenium:在新风中等待页面加载

2024-10-02 04:19:50 发布

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

我已经找到了很多问题的解决方案,我已经在那里找到了很多解决方案。然而,似乎没有一种方法可以有效地将这两种等待结合起来。在

this post的结尾得到了我的主要灵感,我想出了这两个函数,用在“with”语句中:

#This is within a class where the browser is at self.driver

@contextmanager
def waitForLoad(self, timeout=60):
    oldPage = self.driver.find_element_by_tag_name('html')
    yield
    WebDriverWait(self.driver, timeout).until(staleness_of(oldPage))

@contextmanager
def waitForWindow(self, timeout=60):
    oldHandles = self.driver.window_handles
    yield
    WebDriverWait(self.driver, timeout).until(
        lambda driver: len(oldHandles) != len(self.driver.window_handles)
    )

#example
button = self.driver.find_element_by_xpath(xpath)
if button:
    with self.waitForPage():
        button.click()

这些单独使用效果很好。然而,由于每个人检查自身内部条件的方式,它们不能组合在一起。例如,此代码将无法等到第二页加载完毕,因为切换窗口的行为会导致“oldPage”变得过时:

^{pr2}$

有没有一些硒的方法可以让它们协同工作?在


Tags: 方法selfbyisdefdriverwithtimeout

热门问题