selenium 提示 stale element reference: element is not attached to the page document

冯莹發表於2020-07-22

BasePage:
# 視窗切換
def switch_handles(self):
handles = self.driver.window_handles
try:
self.driver.switch_to.window(handles[-1])
logger.info("Switch to window :%s" % self.driver.current_window_handle)
except NameError as e:
logger.error("Failed to switch other window")

Test:
homepage = HomePage(self.driver)
homepage.click_news_link()

newspage = NewsHomePage(self.driver)
newspage.switch_handles()
newspage.click_sports()

報錯:selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
求大神解惑,網上有說重新定位元素,但重新定位不就失去了 po 封裝的意義啦

相關文章