簡體   English   中英

WebdDriverJS / NodeJS:“元素未附加到頁面文檔”錯誤

[英]WebdDriverJS/ NodeJS: 'Element is not attached to the page document' Error

我對WebDriver(chromedriver)完全陌生,並且僅具有javascript語法的超基礎知識,因此,如果我的代碼看起來像意大利面條,對不起。

我有一個clickAllElements(); 查找類中所有元素並單擊它們的函數。

//Click every element of an array
var clickAllElements = function (el, screenShot){
    //Store elements in an array
    var findElements = driver.findElements(By.css(el));
    findElements.then(function (options) {
        //Then iterate through array
        options.map(function (elem) {
            //Click on each item in the array
            return elem.click().then(function() {
                //Then get the text content inside each element
                driver.sleep(5000);
                elem.getText().then(function(text){
                    //Log the text to the console.
                    console.log(" - Selecting " + text.trim());
                     driver.sleep(2000);
                    if (screenShot) {
                        takeScreenShot(text);
                    }                    
                });              
            });
        });
    });
}

這工作正常,但現在我具有以下三個要素。

在此處輸入圖片說明

這是它們的html / css,如Chrome檢查器中所示

在此處輸入圖片說明

我想要我的clickAllElements(); 函數單擊每個值上的x以便將其刪除。 當我clickAllElements('.token > a'); 它實際上確實刪除了第一個,但是之后我得到了“陳舊元素引用:元素未附加到頁面文檔中”。 我不明白該錯誤,因為這些元素顯然存在。 我該如何解決? (最好使用javascripit解決方案);

有兩種可能性:

單擊后立即刪除該元素,但稍后會引用

elem.getText().then(function(text)

在這種情況下,僅在delete鏈接的情況下無需對對象進行進一步引用。 為此功能添加一個參數,或者更好的是,為刪除所有鏈接創建另一個參數。

單擊后元素將更新

您還可以通過編寫另一個函數來解決此問題,但是在這種情況下,您也可以重新編寫該函數以按索引循環遍歷每個元素,每次單擊后刷新元素列表並從索引指定的文本中獲取文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM