Используя Puppeteer, как я могу открыть страницу, получить данные и перейти на предыдущую страницу с этими данными в цикле - PullRequest
0 голосов
/ 16 февраля 2020

Используя Puppeteer, я хотел бы получить все элементы на странице с определенным именем класса, а затем l oop и щелкнуть по каждому из них и go вернуться на другую страницу, нажав на эти данные со страницы.

Я пробовал это:

await page.goto('https://myschool.ng/classroom/christian-religious-knowledge-crk?exam_type=jamb&exam_year=2019', {waitUntil: 'load', timeout: 100000})
  let  questions=  await page.$$('div.media-body') 
  let data = [] 
  await questions.forEach(async (element, i)=>{
         let question =  await  element.$eval(".question-desc.mt-0.mb-3 > p", el=>el.innerHTML)
         let options =   await element.$$eval("ul >li",el=>el.map(option=>option.innerHTML))
          await page.waitFor(2000)

          await element.$eval('a', el=>el.click())
          await page.waitForSelector("h5.text-success.mb-3")
          let correctOption = await page.$eval('h5.text-success.mb-3', el=>el.innerHTML)
          //data.push({question, options, correctOption})
          page.goBack()
          console.log({question, options, correctOption}, i)
    })

, но я всегда получаю это в своем журнале консоли:

   question: 'The core of Saul\'s disobedience was that_____________',
  options:
   [ '\n            <strong>A.</strong>\n            He freed the Kenites\n        ',
     '\n            <strong>B.</strong>\n            He refused to carry out God\'s assignment on eliminating the Amalekites\n        ',
     '\n            <strong>C.</strong>\n            He didn\'t perform the task of smiting Amalekites to the letter\n        ',
     '\n            <strong>D.</strong>\n            He preserved the booty for himself\n        ' ],
  correctOption: 'Correct Answer: Option D' } 0
{ question:
   'God instructed Saul to utterly destroy Amalek because___________',
  options:
   [ '\n            <strong>A.</strong>\n            He abhors Idolatory and all forms of Unrighteousness\n        ',
     '\n            <strong>B.</strong>\n            Its people opposed the Israelites on the way after departing Egypt\n        ',
     '\n            <strong>C.</strong>\n            Its people encouraged Israel to disobey God\n        ',
     '\n            <strong>D.</strong>\n            Samuel had prayed for it destruction\n        ' ],
  correctOption: 'Correct Answer: Option D' } 1
{ question:
   'One of the most significant lessons from the emergence of Deborah as a leader is that_________',
  options:
   [ '\n            <strong>A.</strong>\n            She not only prophesied but also advised Israel\n        ',
     '\n            <strong>B.</strong>\n            Gender difference isn\'t a limitation to leadership\n        ',
     '\n            <strong>C.</strong>\n            Honesty and Integrity are key to leadership\n        ',
     '\n            <strong>D.</strong>\n            One needs to summon courage as a leader to solve problems\n        ' ],
  correctOption: 'Correct Answer: Option D' } 2
{ question:
   'Jeremiah prophesied God\'s punishment of the Israelites because they had_______',
  options:
   [ '\n            <strong>A.</strong>\n            Compromised his worship\n        ',
     '\n            <strong>B.</strong>\n            Committed atrocities with their neighbors\' wives\n        ',
     '\n            <strong>C.</strong>\n            Trampled on the needy\n        ',
     '\n            <strong>D.</strong>\n            No belief in his message\n        ',
     '\n            <strong>D.</strong>\n            No belief in his message\n        ' ],
  correctOption: 'Correct Answer: Option D' } 3
{ question:
   'Jesus healed the lame man who had been sick for thirty-eight years at?',
  options:
   [ '\n            <strong>A.</strong>\n            Capernaum\n        ',
     '\n            <strong>B.</strong>\n            Samaria\n        ',
     '\n            <strong>C.</strong>\n            Galilee\n        ',
     '\n            <strong>D.</strong>\n            Betsheba\n        ' ],
  correctOption: 'Correct Answer: Option D' } 4

Я пробовал все, но правильный вариант не данные, которые я перебираю в сети и показывающие первый вариант.

1 Ответ

0 голосов
/ 16 февраля 2020

Основная причина была в том, что forEach работает асинхронно, и мне нужен l oop для синхронной работы. Окончательное решение -

await page.goto('https://myschool.ng/classroom/christian-religious-knowledge-crk?exam_type=jamb&exam_year=2019', {waitUntil: 'load', timeout: 100000})
  let  questions=  await page.$$('div.media-body') 
  let data = [] 
    for (let index = 0; index < questions.length; index++) {
        let question =  await questions[index].$eval(".question-desc.mt-0.mb-3 > p", el=>el.innerHTML)
        let options =   await questions[index].$$eval("ul >li",el=>el.map(option=>option.innerHTML))
        await questions[index].$eval('.btn', el=>el.click())
        await page.waitFor(2000)
        await page.waitForSelector("h5.text-success.mb-3")
         .then( async()=>{
            console.log(await  page.$eval(" .text-success.mb-3", el=>el.innerHTML))
            page.goBack()
            await page.waitFor(2000)
            questions=  await page.$$('div.media-body') 
         })
         .catch(async ()=>{
            console.log("jsjjs")
             page.goBack()
             await page.waitFor(2000)
           questions=  await page.$$('div.media-body') 
         })
    }

})

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...