StaleElementReferenceError - Selenium Webdriver常見問題之一 (下)


(node:2880) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document

上次講咗話用sleep黎解決StaleElementReferenceError嘅問題,咁有無啲乜嘢係連用sleep都解決唔到呢?
大家可以想像下呢個情境:你想寫一段嘢去令佢會喺Google度搵結果,有相關結果嘅就入去然後再出返黎繼續喺Google度搵。咁好自然地就會用到looping同埋navigate().back()呢兩嚿嘢,如同一開始張圖咁。不過,當瀏覽器返回Google嘅搜尋頁嗰陣就會出現StaleElementReferenceError。點解?

The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page. A less common, but still common cause is where a JS library has deleted an element and replaced it with one with the same ID or attributes. In this case, although the replacement elements may look identical they are different; the driver has no way to determine that the replacements are actually what's expected.

Stale Element Reference Exception
根據SeleniumHQ對於StaleElementReferenceError嘅解釋,瀏覽器無辦法將refresh前後嘅同一個element當做相同嘅元素看待。當瀏覽器返回Google搜尋列表嗰陣,Webdriver無辦法將同一個頁面嘅元素當係之前拎到嘅元素看待。

要解決呢個問題,首先你要有一個大前提係:你返回搜尋列表嘅時候堆資料係同之前一樣。比如個列表有a, b, c呢三個結果,你入咗去a之後再返回都係要顯示返a, b, c呢三個結果。喺呢個前提之下,你就可以寫一個額外嘅function去重新攞過堆元素。當你喺a出返黎之後,就行呢個function去重新reference過啲元素。

const getElement = async (browser, cssSelector, index) => {
    let elements = await browser.findElements(by.css(cssSelector));
    
    return elements[index];
}

用呢個方法就可以重新拎過新嘅元素reference,同時會回傳返你需要嘅元素。咁樣做就可以迴避咗StaleElementReferenceError嘅問題。


Selenium系列:

Powered by Half-moon Production.

Copyright © 2011-2020 by 泣雨家的雜物房. Version 1.5.2.

StaleElementReferenceError - Selenium Webdriver常見問題之一 (下)


(node:2880) UnhandledPromiseRejectionWarning: StaleElementReferenceError: stale element reference: element is not attached to the page document

上次講咗話用sleep黎解決StaleElementReferenceError嘅問題,咁有無啲乜嘢係連用sleep都解決唔到呢?
大家可以想像下呢個情境:你想寫一段嘢去令佢會喺Google度搵結果,有相關結果嘅就入去然後再出返黎繼續喺Google度搵。咁好自然地就會用到looping同埋navigate().back()呢兩嚿嘢,如同一開始張圖咁。不過,當瀏覽器返回Google嘅搜尋頁嗰陣就會出現StaleElementReferenceError。點解?

The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page. A less common, but still common cause is where a JS library has deleted an element and replaced it with one with the same ID or attributes. In this case, although the replacement elements may look identical they are different; the driver has no way to determine that the replacements are actually what's expected.

Stale Element Reference Exception
根據SeleniumHQ對於StaleElementReferenceError嘅解釋,瀏覽器無辦法將refresh前後嘅同一個element當做相同嘅元素看待。當瀏覽器返回Google搜尋列表嗰陣,Webdriver無辦法將同一個頁面嘅元素當係之前拎到嘅元素看待。

要解決呢個問題,首先你要有一個大前提係:你返回搜尋列表嘅時候堆資料係同之前一樣。比如個列表有a, b, c呢三個結果,你入咗去a之後再返回都係要顯示返a, b, c呢三個結果。喺呢個前提之下,你就可以寫一個額外嘅function去重新攞過堆元素。當你喺a出返黎之後,就行呢個function去重新reference過啲元素。

const getElement = async (browser, cssSelector, index) => {
    let elements = await browser.findElements(by.css(cssSelector));
    
    return elements[index];
}

用呢個方法就可以重新拎過新嘅元素reference,同時會回傳返你需要嘅元素。咁樣做就可以迴避咗StaleElementReferenceError嘅問題。


Selenium系列: