segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

Trying to get title from each visited link and check if the title from the result is the same with the title on website page #1567

Open AnnaTykhomyrova opened 5 years ago

AnnaTykhomyrova commented 5 years ago

Hi, everyone! I'm new using nightmare.js. I need to solve exercise below, I have already done something, but still have no idea what to do the next. a). Navigate to Google page. b). Search for 'W3Schools'. c). In the first result page navigate on each link and check if the title from the result is the same with the title on website page. This is my code:

const Nightmare = require('nightmare');     
    (async ()=>{
        let nightmare; 
        try {
    nightmare = Nightmare({ show: true });
    await nightmare
        .goto('https://google.com')
        .type('form[action*="/search"] [name=q]', 'W3Schools')
        .click('form[action*="/search"] [type=submit]')
        .wait(2500)
                .evaluate(links => {
                 var allHrefs = document.querySelectorAll('.rc .r a')
                 var allLinks = []
                 allHrefs.forEach(function(a) {
                 allLinks.push(a.href)
                })
            console.log('allLinks:', allLinks)
            return allLinks
        }, '.what')
        .end()
        .then(result => {
            console.log('links:', result)
            let titles = []
            result.forEach(link => {
                return nightmare
                  .goto(link)
                  .wait('2500')
                  .evaluate(getTitle => {
                    var thisTitle = document.title
                    console.log('this title:', thisTitle)
                    titles.push(thisTitle)
                  })
              })
              console.log('titles:', titles)
              return titles
            })
        } catch (error) {
        console.error(error);
        throw error;
            } finally {
        await nightmare.end();
            }
            })();

It goes to each link, but that's it.

nightmare queuing process start +0ms
  nightmare queueing action "goto" for https://google.com +3ms
  nightmare queueing action "type" +0ms
  nightmare queueing action "click" +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare running +1ms
  nightmare electron child process exited with code 0: success! +6s
result: [ 'https://www.w3schools.com/',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:rSZGNWRvJEkJ:https://www.w3schools.com/+&cd=1&hl=en&ct=clnk&gl=ca',
  'https://www.google.com/search?q=related:https://www.w3schools.com/+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAAegQIBxAG',
  'https://en.wikipedia.org/wiki/W3Schools',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:EIXGT-yolaMJ:https://en.wikipedia.org/wiki/W3Schools+&cd=8&hl=en&ct=clnk&gl=ca',
  'https://www.google.com/search?q=related:https://en.wikipedia.org/wiki/W3Schools+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAHegQIAhAF',
  'https://www.w3schools.in/',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:x1znv-i9iRAJ:https://www.w3schools.in/+&cd=9&hl=en&ct=clnk&gl=ca',
  'https://www.w3schools.com/html/',
  'https://www.w3schools.in/php/',
  'https://www.w3schools.com/about/',
  'https://www.w3schools.com/css/css_intro.asp',
  'https://www.w3schools.in/c-tutorial/',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:2gC9R_B189cJ:https://www.w3schools.in/c-tutorial/+&cd=19&hl=en&ct=clnk&gl=ca',
  'https://www.quanzhanketang.com/html/default.html',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:9j0JBwW9lEYJ:https://www.quanzhanketang.com/html/default.html+&cd=20&hl=en&ct=clnk&gl=ca',
  'https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:o6_MWqgJrNQJ:https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ+&cd=21&hl=en&ct=clnk&gl=ca',
  'https://www.google.com/search?q=related:https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAUegQIBRAF',
  'https://www.w3fools.com/',
  'https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc#',
  'https://webcache.googleusercontent.com/search?q=cache:RabeDrvTtNwJ:https://www.w3fools.com/+&cd=22&hl=en&ct=clnk&gl=ca' ]
  nightmare queueing action "goto" for https://www.w3schools.com/ +5ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:rSZGNWRvJEkJ:https://www.w3schools.com/+&cd=1&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?q=related:https://www.w3schools.com/+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAAegQIBxAG +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://en.wikipedia.org/wiki/W3Schools +1ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:EIXGT-yolaMJ:https://en.wikipedia.org/wiki/W3Schools+&cd=8&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?q=related:https://en.wikipedia.org/wiki/W3Schools+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAHegQIAhAF +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.in/ +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +1ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:x1znv-i9iRAJ:https://www.w3schools.in/+&cd=9&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.com/html/ +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.in/php/ +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.com/about/ +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.com/css/css_intro.asp +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3schools.in/c-tutorial/ +0ms
  nightmare queueing action "wait" +1ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:2gC9R_B189cJ:https://www.w3schools.in/c-tutorial/+&cd=19&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.quanzhanketang.com/html/default.html +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:9j0JBwW9lEYJ:https://www.quanzhanketang.com/html/default.html+&cd=20&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ +1ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:o6_MWqgJrNQJ:https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ+&cd=21&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?q=related:https://www.youtube.com/channel/UCv9bWHC0DIn-Xb7ALNoOGWQ+W3Schools&tbo=1&sa=X&ved=2ahUKEwjdld3quIrkAhVLm-AKHRKJBKgQHzAUegQIBRAF +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.w3fools.com/ +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
  nightmare queueing action "goto" for https://www.google.com/search?source=hp&ei=MDtYXdzAFpK5ggfP-qqgCQ&q=W3Schools&oq=W3Schools&gs_l=psy-ab.12..0l10.254.1057..1167...0.0..0.170.802.9j1......0....1..gws-wiz.......0i131.xJfRw6u2oWI&ved=0ahUKEwjcmJvpuIrkAhWSnOAKHU-9CpQQ4dUDCAc# +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +1ms
  nightmare queueing action "goto" for https://webcache.googleusercontent.com/search?q=cache:RabeDrvTtNwJ:https://www.w3fools.com/+&cd=22&hl=en&ct=clnk&gl=ca +0ms
  nightmare queueing action "wait" +0ms
  nightmare queueing action "evaluate" +0ms
titles: []
  nightmare running +0ms

And I'm not sure how after I can check if the title from the result is the same with the title on website page. Does anybody has some ideas? Thanks for any help.