下载似乎无法启动

时间:2018-02-26 08:30:05

标签: javascript puppeteer

我尝试通过保存按钮下载带有puppeeter的文件,该保存按钮在单击按钮导出(javascript)后显示

示例网址https://fr.semrush.com/fr/info/cinema+(full+search)

enter image description here

因此,对于puppeeter,第一次点击动作(图片中的红色)工作。 英语中“sauvegarder”或“save”的第二次单击操作不起作用(事实上,如果您正常使用它,则文件正在下载)。

这是我的完整代码(适用于法语版本的fsemrush)

// Import package
const fs = require('fs');
const rl = require('readline');
const puppeteer = require('puppeteer')

// **** function to read list of keywords then extract by download
// **** file must be encodeed in UTF8 BOM mode


// ***** semrush login
// ***** this function generates the way of the cookies cramble for all futur actions on semrush 
const semrushlog = async () => {

  // we create a chrome session - set headless to true if you don't want to see the browser pop-up
  const browser = await puppeteer.launch({ headless: false })

  // we setup a new tab in our browser - here we go mario (wouhouu)
  const page = await browser.newPage()

  try {

      // define the browser size on screen - important if you wana screenshot for example ;)
      //await page.setViewport({ width: 1000, height: 500 })

      // navigate to semrush home
      await page.goto('https://fr.semrush.com/fr/info/cinema+(full+search)')


      //page result button
      const exportBtn = '#PhraseDetailed > div.sem-widget-header > div > div.sem-widget-footer-rb.sem-widget-footer-export-links > div > button'
      const downloadBtn = '#PhraseDetailed > div.sem-widget-header > div > div.sem-widget-footer-rb.sem-widget-footer-export-links > div > div > div.s-dropdown__block.clearfix > button > span'



      // load page search keywords 





        //debug -- check number of page in array
        //console.log(generatedPages[0])

        // get our semrush data
        var generatedPageLength = generatedPages.length

        await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: 'C:/QCD/puppeteer/'})


            console.log(generatedPages[i])

            await page.goto(generatedPages[i])
            //download the file
            await page.click(exportBtn)

            await page.waitFor(2*1000)

            // **** btn download doesn't fire the file download
            // ****
            await page.click(downloadBtn)


            //page.waitForNavigation('load')
            //wait 5s
            await page.waitFor(15*1000)



  } catch (err) {
        console.log("caught an exception", err);
        await browser.close();
        return
    }

}



// call our semrush function
semrushlog()

提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

// Import package
const fs = require('fs');
const rl = require('readline');
const puppeteer = require('puppeteer')

// **** function to read list of keywords then extract by download
// **** file must be encodeed in UTF8 BOM mode
// ***** semrush login
// ***** this function generates the way of the cookies cramble for all futur actions on semrush 

const semrushlog = async () => {
  // we create a chrome session - set headless to true if you don't want to see the browser pop-up
  const browser = await puppeteer.launch({ headless: false })

  // we setup a new tab in our browser - here we go mario (wouhouu)
  const page = await browser.newPage()

  try {

      // define the browser size on screen - important if you wana screenshot for example ;)
      //await page.setViewport({ width: 1000, height: 500 })

      // navigate to semrush home
      await page.goto('https://fr.semrush.com/fr/info/cinema+(full+search)')


      //page result button
      const exportBtn = '#PhraseDetailed > div.sem-widget-header > div > div.sem-widget-footer-rb.sem-widget-footer-export-links > div > button'
      const downloadBtn = '#PhraseDetailed > div.sem-widget-header > div > div.sem-widget-footer-rb.sem-widget-footer-export-links > div > div > div.s-dropdown__block.clearfix > button > span'

      // load page search keywords 
        //debug -- check number of page in array
        //console.log(generatedPages[0])
        // get our semrush data
        var generatedPageLength = generatedPages.length
        await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: 'C:/QCD/puppeteer/'})
            console.log(generatedPages[i])
            await page.goto(generatedPages[i])
            //download the file
            await page.click(exportBtn)
            await page.waitFor(2*1000)
            // **** btn download doesn't fire the file download
            // ****
            await page.click(downloadBtn)
            //page.waitForNavigation('load')
            //wait 5s
            await page.waitFor(15*1000)
  } catch (err) {
        console.log("caught an exception", err);
        await browser.close();
        return
    }

}


(async () => {
    // call our semrush function
    await semrushlog()
})();