【问题标题】:Why is my script not iterating correctly?为什么我的脚本不能正确迭代?
【发布时间】:2020-08-14 18:25:46
【问题描述】:

脚本运行良好,但我希望它运行不止一次。我把我认为完全正常的基本 for 循环放在了整个脚本的顶部。

而不是多次运行脚本,它只是将脚本的每个动作加倍,例如。如果我将迭代变量设置为 2,它只会通过脚本执行每个操作两次,而我希望它从头到尾运行整个脚本两次。

const puppeteer = require('puppeteer');
const C = require('./constants');
var USERNAME_SELECTOR;
var PASSWORD_SELECTOR;
var PHONE_SELECTOR;
var CTA_SELECTOR;
const URLL = process.argv[2];
var iterations = process.argv[3];
var usernameFieldWasFound = false;
var passwordFieldWasFound = false;
var buttonWasFound = false;
var phoneFieldWasFound = false;

for (i = 0; i < iterations; i++) {


async function startBrowser() {
  const browser = await puppeteer.launch({
  headless: false})
  const page = await browser.newPage();
  return {browser, page};
}


async function closeBrowser(browser) {
  return browser.close();
}



(async () => {
  await playTest(URLL);
  process.exit(1);
})();


async function playTest(url) {
  const {browser, page} = await startBrowser();
  page.setViewport({width: 1366, height: 768});
  await page.goto(url);

  await delay(1500);



  // // creates a map of all links on the page
  // const allLinks = await page.$$eval('a', as => as.map(a => a.href));

  // // displays the number of links on the page
  // console.log(allLinks.length);

  // // contains the first link
  // // console.log(allLinks[0]);

  // console.log(allLinks);



  // searches for a username field, if statements are in order of likliehood of being the correct selector (least to most)

  if (await page.$('[type="text"]') !== null) {
    USERNAME_SELECTOR = '[type="text"]' 
  console.log ('Changed username field selector to "[type=text]"')
  usernameFieldWasFound = true; }  

  if (await page.$('#si_username') !== null) {
    USERNAME_SELECTOR = '#si_username';
    console.log ('Changed username field selector to #si_username')
    usernameFieldWasFound = true; }
 
  if (await page.$('[name="username"]') !== null) {
    USERNAME_SELECTOR = '[name="username"]' 
  console.log ('Changed username field selector to name=username')
  usernameFieldWasFound = true; }

  if (await page.$('#username') !== null) {
    USERNAME_SELECTOR = '#username';
    console.log ('Changed username field selector to #username')
    usernameFieldWasFound = true; }

  if (await page.$('#email') !== null) {
    USERNAME_SELECTOR = '#email';
    console.log ('Changed username field selector to #email')
    usernameFieldWasFound = true; }

    if (await page.$('[placeholder="Enter your username"]') !== null) {
      USERNAME_SELECTOR = '[placeholder="Enter your username"]' 
    console.log ('Changed username field selector to "[placeholder="Enter your username"]"')
    usernameFieldWasFound = true; } 
    
    if (await page.$('[placeholder="Username"]') !== null) {
      USERNAME_SELECTOR = '[placeholder="Username"]' 
    console.log ('Changed username field selector to "[placeholder="Username"]"')
    usernameFieldWasFound = true; } 
    
    if (await page.$('[placeholder="Email"]') !== null) {
        USERNAME_SELECTOR = '[placeholder="Email"]' 
      console.log ('Changed username field selector to "[placeholder="Email"]"')
      usernameFieldWasFound = true; }   

    if (await page.$('[placeholder="Please enter your username"]') !== null) {
      USERNAME_SELECTOR = '[placeholder="Please enter your username"]'
    console.log ('Changed username field selector to "[placeholder="Please enter your username"]"')
    usernameFieldWasFound = true; }  

    if (await page.$('[placeholder="Please enter your email"]') !== null) {
      USERNAME_SELECTOR = '[placeholder="Please enter your email"]'
    console.log ('Changed username field selector to "[placeholder="Please enter your email"]"')
    usernameFieldWasFound = true; }  

    if (await page.$('[placeholder="Please enter your email address"]') !== null) {
      USERNAME_SELECTOR = '[placeholder="Please enter your email address"]'
    console.log ('Changed username field selector to "[placeholder="Please enter your email address"]"')
    usernameFieldWasFound = true; }  

    if (await page.$('[type="email"]') !== null) {
      USERNAME_SELECTOR = '[type="email"]' 
    console.log ('Changed username field selector to "[type=email]"')
    usernameFieldWasFound = true; } 

    if (await page.$('[data-val-required="Please enter your email"]') !== null) {
        USERNAME_SELECTOR = '[data-val-required="Please enter your email"]' 
      console.log ('Changed username field selector to "[data-val-required="Please enter your email"]')
      usernameFieldWasFound = true; } 



    // failsafe incase username field is found but is not a real username field and there is a button
    // which is more likely to lead to a real login form
      if (await page.$('[class="mail-selector"]') !== null) {
      usernameFieldWasFound = false; }   




    // If no username field was found on the page this will trigger. 
    // The script should then click on any buttons with names that could lead to a login form and check again
    // This part of the script searches for buttons
  if (usernameFieldWasFound == false) {
    console.log('No username field was found, exploring buttons...')

    if (await page.$([type="submit"]) !== null) {
      CTA_SELECTOR = [type="submit"];
      console.log ('Changed button selector type=submit')
      console.log ('Button was found')
      buttonWasFound = true;
    }
  
     if (await page.$('[type="submit"]') !== null) {
      CTA_SELECTOR = '[type="submit"]' 
    console.log ('Changed button selector to "type=submit"')
    console.log ('Button was found')
    buttonWasFound = true;}
  
  
    if (await page.$('#loginbutton') !== null) {
      CTA_SELECTOR = '#loginbutton' 
      console.log ('Changed button selector to #loginbutton')
      console.log ('Button was found')
      buttonWasFound = true;}
  
  
    if (await page.$('#submit-btn') !== null) {
      CTA_SELECTOR = '#submit-btn' 
      console.log ('Changed button selector to #submit-btn')
      console.log ('Button was found')
      buttonWasFound = true;}

    if (await page.$('[class="mail-selector"]') !== null) {
      CTA_SELECTOR = '[class="mail-selector"]';
      console.log ('Changed button selector to class=mail-selector')
      console.log ('Button was found')
      buttonWasFound = true;}

      if (await page.$('[data-ga-slug="login"]') !== null) {
        CTA_SELECTOR = '[data-ga-slug="login"]';
        console.log ('Changed button selector to [data-ga-slug="login"]')
        console.log ('Button was found')
        buttonWasFound = true;}
  }  
    // if a username field was found sets boolean to true
  if (usernameFieldWasFound == true) {
    buttonWasFound = true;
  }
    // if no plausible button was found the script has failed and will/should end here. 
  if (buttonWasFound == false) {
    console.log('No login form could be found on this URL or the most plausible button leading to a login form on this URL.');
    await browser.close();
  }

  // username field was not found however a plausible button was found which may lead to a username field
  // Therefore button will be clicked and username field should be searched for on next page
  if (usernameFieldWasFound == false && buttonWasFound == true) {
    console.log('Username field was not found on first page but a pluasible button was found')
    console.log('Clicking button...')
    await page.waitForSelector(CTA_SELECTOR);
  await page.click(CTA_SELECTOR);
 
  // giving page time to load possible new login form
  await delay(1000);

    // searches for a username field on new page
    if (await page.$('[type="text"]') !== null) {
        USERNAME_SELECTOR = '[type="text"]' 
      console.log ('Changed username field selector to "[type=text]"')
      usernameFieldWasFound = true; }  
    
      if (await page.$('#si_username') !== null) {
        USERNAME_SELECTOR = '#si_username';
        console.log ('Changed username field selector to #si_username')
        usernameFieldWasFound = true; }
     
      if (await page.$('[name="username"]') !== null) {
        USERNAME_SELECTOR = '[name="username"]' 
      console.log ('Changed username field selector to name=username')
      usernameFieldWasFound = true; }
    
      if (await page.$('#username') !== null) {
        USERNAME_SELECTOR = '#username';
        console.log ('Changed username field selector to #username')
        usernameFieldWasFound = true; }
    
      if (await page.$('#email') !== null) {
        USERNAME_SELECTOR = '#email';
        console.log ('Changed username field selector to #email')
        usernameFieldWasFound = true; }
    
        if (await page.$('[placeholder="Enter your username"]') !== null) {
          USERNAME_SELECTOR = '[placeholder="Enter your username"]' 
        console.log ('Changed username field selector to "[placeholder="Enter your username"]"')
        usernameFieldWasFound = true; } 
        
        if (await page.$('[placeholder="Username"]') !== null) {
          USERNAME_SELECTOR = '[placeholder="Username"]' 
        console.log ('Changed username field selector to "[placeholder="Username"]"')
        usernameFieldWasFound = true; } 
        
        if (await page.$('[placeholder="Email"]') !== null) {
            USERNAME_SELECTOR = '[placeholder="Email"]' 
          console.log ('Changed username field selector to "[placeholder="Email"]"')
          usernameFieldWasFound = true; }   
    
        if (await page.$('[placeholder="Please enter your username"]') !== null) {
          USERNAME_SELECTOR = '[placeholder="Please enter your username"]'
        console.log ('Changed username field selector to "[placeholder="Please enter your username"]"')
        usernameFieldWasFound = true; }  
    
        if (await page.$('[placeholder="Please enter your email"]') !== null) {
          USERNAME_SELECTOR = '[placeholder="Please enter your email"]'
        console.log ('Changed username field selector to "[placeholder="Please enter your email"]"')
        usernameFieldWasFound = true; }  
    
        if (await page.$('[placeholder="Please enter your email address"]') !== null) {
          USERNAME_SELECTOR = '[placeholder="Please enter your email address"]'
        console.log ('Changed username field selector to "[placeholder="Please enter your email address"]"')
        usernameFieldWasFound = true; }  
    
        if (await page.$('[type="email"]') !== null) {
          USERNAME_SELECTOR = '[type="email"]' 
        console.log ('Changed username field selector to "[type=email]"')
        usernameFieldWasFound = true; } 
    
        if (await page.$('[data-val-required="Please enter your email"]') !== null) {
            USERNAME_SELECTOR = '[data-val-required="Please enter your email"]' 
          console.log ('Changed username field selector to "[data-val-required="Please enter your email"]')
          usernameFieldWasFound = true; } 

    // now username selector is either changed to correct one, or not found. If it's changed to correct we can continue with script
    // if it's not found we give up

    if (usernameFieldWasFound == false) {
      console.log('No username field was found after clicking plausible button. Script over');
      await browser.close();
    }

  
  
    }

 

  await page.waitForSelector(USERNAME_SELECTOR);
  console.log('Clicking username selector');
  await page.click(USERNAME_SELECTOR);
  console.log('Inputting fake username');
  await page.keyboard.type(C.username, {delay: 100});




// If clicking a next button after inputting username is required this will trigger here. 
  if (await page.$('#next') !== null) {
    CTA_SELECTOR = '#next';
    console.log ('Changed button selector to #next')
    console.log('Clicking button.')
    await page.click(CTA_SELECTOR);}







  if (await page.$('#si_password') !== null) {
    PASSWORD_SELECTOR = '#si_password';
    console.log ('Changed password field selector to #si_password')
    passwordFieldWasFound = true;}

  if (await page.$('#password') !== null) {
    PASSWORD_SELECTOR = '#password';
    console.log ('Changed password field selector to #password')
    passwordFieldWasFound = true;}

  if (await page.$('#pass') !== null) {
    PASSWORD_SELECTOR = '#pass';
    console.log ('Changed password field selector to #pass')
    passwordFieldWasFound = true;}

  if (await page.$('[type="password"]') !== null) {
    PASSWORD_SELECTOR = '[type="password"]' 
  console.log ('Changed password field selector to type=password .')
  passwordFieldWasFound = true;}

  if (await page.$('[placeholder="Enter your password"]') !== null) {
    PASSWORD_SELECTOR = '[placeholder="Enter your password"]' 
  console.log ('Changed password field selector to [placeholder="Enter your password"] .')
  passwordFieldWasFound = true;}

  // In the unlikely event that no password field can be found the script will end here.
if (passwordFieldWasFound == false) {
    console.log("A password field could not be found. Script has failed to send fake credentials.")
    await delay(3500);
    await browser.close();
}

  console.log('Clicking password selector.')
  await page.click(PASSWORD_SELECTOR);
  console.log('Inputting fake password');
  await page.keyboard.type(C.password, {delay: 100});

  await delay(1000);



// phone number

if (await page.$('[name="phn"]') !== null) {
  PHONE_SELECTOR = '[name="phn"]' 
console.log ('Changed username field selector to [name="phn"] .')
phoneFieldWasFound = true; }

if (await page.$('[name="phone"]') !== null) {
  PHONE_SELECTOR = '[name="phone"]' 
console.log ('Changed username field selector to [name="phone"] .')
phoneFieldWasFound = true; }

if (await page.$('[name="phonenumber"]') !== null) {
  PHONE_SELECTOR = '[name="phonenumber"]' 
console.log ('Changed username field selector to [name="phonenumber"]')
phoneFieldWasFound = true; }

if (await page.$('[name="number"]') !== null) {
  PHONE_SELECTOR = '[name="number"]' 
console.log ('Changed username field selector to [name="number"]')
phoneFieldWasFound = true; }

if (await page.$('[placeholder="Enter your phone number"]') !== null) {
  PHONE_SELECTOR = '[placeholder="Enter your phone number"]' 
console.log ('Changed username field selector to "[placeholder="Enter your phone number"]"')
phoneFieldWasFound = true; }

if (await page.$('[placeholder="Please enter your phone number"]') !== null) {
  PHONE_SELECTOR = '[placeholder="Please Enter your phone number"]' 
console.log ('Changed username field selector to [placeholder="Please Enter your phone number"]')
phoneFieldWasFound = true; }



if (phoneFieldWasFound == true) {

  console.log('Clicking phone selector.')  
  await page.click(PHONE_SELECTOR);
  console.log('Inputting fake phone number') 
  await page.keyboard.type(C.phone, {delay: 100});

}



if (await page.$('[type="button"]') !== null) {
  CTA_SELECTOR = '[type="button"]' 
console.log ('Changed button selector to type=button ')}


if (await page.$('[name="Login"]') !== null) {
    CTA_SELECTOR = '[name="Login"]'
  console.log ('Changed button selector to "[name="Login"]"')}


  if (await page.$('#loginbutton') !== null) {
    CTA_SELECTOR = '#loginbutton' 
    console.log ('Changed button selector to #loginbutton')}


  if (await page.$('#submit-btn') !== null) {
    CTA_SELECTOR = '#submit-btn' 
    console.log ('Changed button selector to #submit-btn')}

    if (await page.$([type="submit"]) !== null) {
      CTA_SELECTOR = [type="submit"];
      console.log ('Changed button selector type=submit')}

     if (await page.$('[type="submit"]') !== null) {
      CTA_SELECTOR = '[type="submit"]' 
    console.log ('Changed button selector to type=submit ')}

    if (await page.$('[href="javascript:login();"]') !== null) {
        CTA_SELECTOR = '[href="javascript:login();"]' 
      console.log ('Changed button selector to "[href="javascript:login();"]" ')}

    if (await page.$('[id="login-submit-button"]') !== null) {
        CTA_SELECTOR = '[id="login-submit-button"]'
      console.log ('Changed button selector to "[id="login-submit-button"]" ')}

    


 


   
  console.log('Clicking submit button') 
  await page.click(CTA_SELECTOR);
  console.log('Sucessfully sent fake credentials.')
  delay(3500);
  // 2nd click as some submit buttons will fail to send credentials with one click, this may be because some phishing kits
  // will arbitrarily tell the user that their credentials are invaid after clicking once and only accept creds on 2nd click



  if (await page.$(CTA_SELECTOR) !== null) {
  page.click(CTA_SELECTOR); 
  console.log ('Reclicked button selector')}

  //  await page.screenshot({path: 'screenshot.png'});

  browser.close();



function delay(time) {
  return new Promise(function(resolve) { 
      setTimeout(resolve, time)
  });
}}

}



【问题讨论】:

    标签: javascript node.js loops puppeteer


    【解决方案1】:

    您所做的是声明并立即运行所有函数iterations 次,并且它们并行运行。

    尝试将所有内容声明一次,然后只运行主函数需要多少次:

    (async () => {
      for (i = 0; i < iterations; i++) {
        await playTest(URLL);
      }
      process.exit(1);
    })();
    

    另外请注意,process.exit(1) 中的 1 表示“错误”,而这基本上是指“脚本失败”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-15
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多