簡體   English   中英

噩夢 - 避免多次使用單擊並等待 [暫停]

[英]nightmare - Avoiding multiple use click and wait [on hold]

如何避免在此代碼中多次使用單擊等待 如果我有大量它,代碼會很丑陋。 有沒有辦法通過循環來做到這一點?

var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
});

nightmare
    .goto('MY_SITE_ADDRESS')    
    .click('div[id="feed_item_0"]')
    .wait('div[id="accordion_wide_0"] > div > div > div > div > div > div > div > div > div > span')
    .click('div[id="feed_item_1"]')
    .wait('div[id="accordion_wide_1"] > div > div > div > div > div > div > div > div > div > span')
    .click('div[id="feed_item_2"]')
    .wait('div[id="accordion_wide_2"] > div > div > div > div > div > div > div > div > div > span')
    .click('div[id="feed_item_3"]')
    .wait('div[id="accordion_wide_3"] > div > div > div > div > div > div > div > div > div > span')
    .click('div[id="feed_item_4"]')
    .wait('div[id="accordion_wide_4"] > div > div > div > div > div > div > div > div > div > span')
    .evaluate(function () {        
        data = document.querySelectorAll('div[id^="accordion_wide_"]');               
        myArray = [];
        data.forEach(element => {
            myArray.push(element.textContent);    
        });        
        return myArray;
    })        
    .then(function (data) {
        console.log(data);            
    })
    .catch(function (error) {
        console.error(error);
    });

如果您希望保持鏈接,您可以編寫一個自定義插件,如下所示。

var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
});

function expand (id) {
    return function (nightmare) {
        return nightmare
            .click('div[id="feed_item_' + id + '"]')
            .wait('div[id="accordion_wide_' + id + '"] > div > div > div > div > div > div > div > div > div > span');
    };
}

nightmare
    .goto('MY_SITE_ADDRESS')
    .use(expand(0))
    .use(expand(1))
    .use(expand(2))
    .use(expand(3))
    .use(expand(4))
    .evaluate(function () {        
        data = document.querySelectorAll('div[id^="accordion_wide_"]');               
        myArray = [];
        data.forEach(element => {
            myArray.push(element.textContent);    
        });        
        return myArray;
    })        
    .then(function (data) {
        console.log(data);            
    })
    .catch(function (error) {
        console.error(error);
    });

暫無
暫無

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

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