segment-boneyard / nightmare

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

document.querySelector inside evaluate() returning empty objects. But document.documentElement.innerHTML returning correct html. #1500

Open ashwinYardi opened 6 years ago

ashwinYardi commented 6 years ago

Here is the code snippet:

nightmare
    .on('console', (log, msg) => {
        console.log(msg)
    })
    .on('error', (err) => {
        console.log(err)
    })
    .goto(url)
    .inject('js', 'jquery.min.js')
    .wait('#btnSearchClubs')
    .click('#btnSearchClubs')
    .wait(5000)
    .evaluate(function () {
        const pageAnchor = Array.from(document.querySelectorAll('#club-results-container > table:nth-child(2) > tbody > tr > td:nth-child(2) > a:nth-child(11)'));
        console.log(pageAnchor);  // This is empty.
        console.log($('.RVDataGridItem'));   // This is empty.
        pageAnchor[0].addClass('to-be-clicked');
    })
    .click('.to-be-clicked')
    .wait()
    .evaluate(function () {
        return document.documentElement.innerHTML;
    })
    .end()
ashwinYardi commented 6 years ago

Also, in { show: true } mode, dev tools inside electron broswer console successfully prints all logs and elements returned by querySelector. However, console.log in node's scope prints empty objects. Please help guys!