如何使用节点js程序中任何元素的值?

时间:2019-05-30 07:14:23

标签: javascript node.js puppeteer

Element (<input id="pagenum-input" class="form-control" aria-label="..." value="1" min="1" max="28">)

我想使用max(28)的值作为我在第二个循环中创建的变量max的值。我如何从输入标签到程序获取max的值? 任何帮助,将不胜感激。谢谢!

const puppeteer = require('puppeteer');

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

  for (let j=2; j<=6; j++) {
    await page.setViewport({width: 2000, height: 2500})
    await page.goto('http://epaper.freepressjournal.in/');
    await page.click('body > div > div.common_section > div > div > div.rw_rightcrousel > div > div:nth-child('+j+') > a > div > img');  
    await page.waitFor(2000);
    await page.click('#left-chunk-1 > img');
    var maximum; 
    for (let i= 1; i<=maximum; i++) {
      await page.waitFor(2000);
      await page.screenshot({path: '//192.168.2.19/em/automatic downloading/Puppeteer/freepressjournal/'+j+''+i+'.png'});
      await page.waitFor(2000);    
      await page.click('#page-div > a.right_arrow_pgright.btn_next');      
    }
  }

  await browser.close();
}

getPic();

// This loop will automatically get the value of max that is 28.
// The value of max will vary according to pages. The program should accept
// the value of max in maximum and run the loop accordingly.
for (let i= 1; i<=maximum; i++) 

1 个答案:

答案 0 :(得分:0)

您可以借助page.evaluate函数与页面上的任何元素进行交互。您可以通过以下方法找到感兴趣的输入元素并访问其属性之一:

var maximum = await page.evaluate(() => {
    return document.getElementById("pagenum-input").getAttribute("max");
});