Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的脚本没有正确迭代?_Javascript_Node.js_Loops_Puppeteer - Fatal编程技术网

Javascript 为什么我的脚本没有正确迭代?

Javascript 为什么我的脚本没有正确迭代?,javascript,node.js,loops,puppeteer,Javascript,Node.js,Loops,Puppeteer,脚本工作正常,但我希望它运行不止一次。我把我认为是完全正常的基本for循环放在整个脚本的顶部 与多次运行脚本不同,它只是将脚本的每个动作加倍,例如,如果我将迭代变量设置为2,它将只对脚本执行每个动作两次,而我希望它从头到尾运行整个脚本两次 const puppeteer = require('puppeteer'); const C = require('./constants'); var USERNAME_SELECTOR; var PASSWORD_SELECTOR; var PHONE_

脚本工作正常,但我希望它运行不止一次。我把我认为是完全正常的基本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)
  });
}}

}



const puppeter=require('puppeter');
常数C=要求('./常数');
var-u选择器;
var密码选择器;
无功功率选择器;
var-CTA_选择器;
const URLL=process.argv[2];
var迭代次数=process.argv[3];
var usernameFieldWasFound=false;
var passwordFieldWasFound=false;
var buttonWasFound=false;
var phoneFieldWasFound=false;
对于(i=0;i{
等待播放测试(URLL);
过程。退出(1);
})();
异步函数播放测试(url){
const{browser,page}=wait startBrowser();
setViewport({宽度:1366,高度:768});
等待页面。转到(url);
等待延迟(1500);
////创建页面上所有链接的映射
//const allLinks=wait page.$$eval('a',as=>as.map(a=>a.href));
////显示页面上的链接数
//console.log(allLinks.length);
////包含第一个链接
////console.log(所有链接[0]);
//console.log(所有链接);
//搜索用户名字段,如果语句按正确选择器的可能性排序(从最小到最大)
如果(等待页面。$('[type=“text”]')!==null){
用户名\选择器='[type=“text”]'
console.log('将用户名字段选择器更改为“[type=text]”)
usernameFieldWasFound=true;}
如果(等待页面。$('#si_用户名')!==null){
USERNAME_SELECTOR='#si_USERNAME';
console.log('将用户名字段选择器更改为#si_用户名')
usernameFieldWasFound=true;}
如果(等待页面。$('[name=“username”]')!==null){
用户名\选择器='[name=“USERNAME”]'
console.log('将用户名字段选择器更改为name=username')
usernameFieldWasFound=true;}
如果(等待页面。$(“#用户名”)!==null){
USERNAME_选择器='#USERNAME';
console.log('将用户名字段选择器更改为#用户名')
usernameFieldWasFound=true;}
如果(等待页面。$(“#电子邮件”)!==null){
用户名_选择器='#电子邮件';
console.log('将用户名字段选择器更改为#email')
usernameFieldWasFound=true;}
如果(等待页面。$('[placeholder=“Enter your username”]')!==null){
用户名\选择器='[placeholder=“输入您的用户名”]'
console.log('将用户名字段选择器更改为“[placeholder=”输入您的用户名“]”)
usernameFieldWasFound=true;}
如果(等待页面。$('[placeholder=“Username”]')!==null){
用户名\选择器='[placeholder=“USERNAME”]'
console.log('将用户名字段选择器更改为“[placeholder=“username”]”)
usernameFieldWasFound=true;}
如果(等待页面。$('[placeholder=“Email”]')!==null){
用户名\选择器='[placeholder=“Email”]'
console.log('将用户名字段选择器更改为“[placeholder=“Email”]”)
usernameFieldWasFound=true;}
如果(等待页面。$('[placeholder=“Please enter your username”]')!==null){
用户名\选择器='[placeholder=“请输入您的用户名”]'
console.log('将用户名字段选择器更改为“[placeholder=”请输入您的用户名“]”)
usernameFieldWasFound=true;}
如果(等待页面。$('[placeholder=“Please enter your email”]')!==null){
用户名\选择器='[placeholder=“请输入您的电子邮件”]'
console.log('将用户名字段选择器更改为“[placeholder=”请输入您的电子邮件“]”)
usernameFieldWasFound=true;}
如果(等待页面。$(“[placeholder=”请输入您的电子邮件地址“])!==null){
用户名\选择器='[placeholder=“请输入您的电子邮件地址”]'
console.log('将用户名字段选择器更改为“[placeholder=”请输入您的电子邮件地址“]”)
usernameFieldWasFound=true;}
如果(等待页面。$('[type=“email”]')!==null){
用户名\选择器='[type=“email”]'
console.log('将用户名字段选择器更改为“[type=email]”)
usernameFieldWasFound=true;}
如果(等待页面。$(“[data val required=”请输入您的电子邮件“])!==null){
USERNAME_SELECTOR='[data val required=“请输入您的电子邮件”]'
console.log('将用户名字段选择器更改为“[data val required=”请输入您的电子邮件“]”)
usernameFieldWasFound=true;}
//找到了failsafe incase用户名字段,但不是真正的用户名字段,并且有一个按钮
//哪一种更可能导致真正的登录表单
如果(等待页面。$('[class=“mail selector”]')!==null){
usernameFieldWasFound=false;}
//如果在页面上未找到用户名字段,将触发此操作。
//然后,脚本应该单击任何带有可能导致登录表单的名称的按钮,然后再次检查
//脚本的这一部分将搜索按钮
if(usernameFieldWasFound==false){
log('未找到用户名字段,正在搜索按钮…')
如果(等待页面。$([type=“submit”])!==null){
CTA_选择器=[type=“submit”];
console.log('Changed button selector type=submit')
console.log('找到按钮')
buttonWasFound=true;
}
如果(等待页面。$('[type=“submit”]')!==null){
CTA_选择器='[type=“submit”]'
console.log('将按钮选择器更改为“type=submit”')
console.log('找到按钮')
buttonWasFound=true;}
如果(等待页面。$(“#登录按钮”)!==null){
CTA_选择器=“#登录按钮”
console.log('更改按钮
(async () => {
  for (i = 0; i < iterations; i++) {
    await playTest(URLL);
  }
  process.exit(1);
})();