如何重新打开浏览器以使用其他帐户?

时间:2020-03-22 18:56:25

标签: javascript nightmare

我有一些代码。

    for (let i = 1; i < 5; i++){
        login(login, pass);
    }

    function login(login, pass){
        nightmare
            .goto(url)
            .wait('body')
            .evaluate(() => document.querySelector('body').innerHTML)
            ...
    }

但是在第一个帐户浏览器关闭之后。如何重新打开它并使用第二个帐户?

1 个答案:

答案 0 :(得分:0)

以下是实现此目标的方法。

//create an array with the list of URL's you want to navigate
let urls = ['www.google.com', 'www.yahoo.com', 'www.facebook.com'];

//iterate over the array using Array.reduce
//for more details on to how array reduce function works refer google.com

urls.reduce((Accumulator, CurrentValue, CurrentIndex, Arr) =>
{
      
          Accumulator.then(() => {
              return nightmare
                       .goto(CurrentValue) //www.google.com in first iteration
                       .wait(10000) // wait for 10 sec to load page
                       .evaluate(() => {
                        
                           //extract data
                           return result;  //this gets returned to then 
                                           //function in next step                          

                        })
                        .then((result) => {
                            //save result to file or do as per requirement
                         })
          }    

}, Promise.resolve());

上面的代码将遍历数组,并将导航到提到的每个URL 在数组中。

相关问题