Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

node.js - Puppeteer in NodeJS reports 'Error: Node is either not visible or not an HTMLElement'

I'm using 'puppeteer' for NodeJS to test a specific website. It seems to work fine in most case, but some places it reports:

Error: Node is either not visible or not an HTMLElement

The following code picks a link that in both cases is off the screen.

The first link works fine, while the second link fails.

What is the difference? Both links are off the screen.

Any help appreciated, Cheers, :)

Example code

const puppeteer = require('puppeteer');

const initialPage = 'https://statsregnskapet.dfo.no/departementer';
const selectors = [
    'div[id$="-bVMpYP"] article a',
    'div[id$="-KcazEUq"] article a'
];

(async () => {
    let selector, handles, handle;
    const width=1024, height=1600;
    const browser = await puppeteer.launch({ 
        headless: false, 
        defaultViewport: { width, height } 
    });
    const page = await browser.newPage();
    await page.setViewport({ width, height});
    page.setUserAgent('UA-TEST');

    // Load first page
    let stat = await page.goto(initialPage, { waitUntil: 'domcontentloaded'});

    // Click on selector 1 - works ok
    selector = selectors[0];
    await page.waitForSelector(selector);
    handles = await page.$$(selector);
    handle = handles[12]
    console.log('Clicking on: ', await page.evaluate(el => el.href, handle));
    await handle.click();  // OK

    // Click that selector 2 - fails
    selector = selectors[1];
    await page.waitForSelector(selector);
    handles = await page.$$(selector);
    handle = handles[12]
    console.log('Clicking on: ', await page.evaluate(el => el.href, handle));
    await handle.click();  // Error: Node is either not visible or not an HTMLElement

})();

I'm trying to emulate the behaviour of a real user clicking around the site, which is why I use .click(), and not .goto(), since the a tags have onclick events.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First and foremost, your defaultViewport object that you pass to puppeteer.launch() has no keys, only values.

You need to change this to:

'defaultViewport' : { 'width' : width, 'height' : height }

The same goes for the object you pass to page.setViewport().

You need to change this line of code to:

await page.setViewport( { 'width' : width, 'height' : height } );

Third, the function page.setUserAgent() returns a promise, so you need to await this function:

await page.setUserAgent( 'UA-TEST' );

Furthermore, you forgot to add a semicolon after handle = handles[12].

You should change this to:

handle = handles[12];

Additionally, you are not waiting for the navigation to finish (page.waitForNavigation()) after clicking the first link.

After clicking the first link, you should add:

await page.waitForNavigation();

I've noticed that the second page sometimes hangs on navigation, so you might find it useful to increase the default navigation timeout (page.setDefaultNavigationTimeout()):

page.setDefaultNavigationTimeout( 90000 );

Once again, you forgot to add a semicolon after handle = handles[12], so this needs to be changed to:

handle = handles[12];

It's important to note that you are using the wrong selector for your second link that you are clicking.

Your original selector was attempting to select elements that were only visible to xs extra small screens (mobile phones).

You need to gather an array of links that are visible to your viewport that you specified.

Therefore, you need to change the second selector to:

div[id$="-KcazEUq"] article .dfo-widget-sm a

You should wait for the navigation to finish after clicking your second link as well:

await page.waitForNavigation();

Finally, you might also want to close the browser (browser.close()) after you are done with your program:

await browser.close();

Note: You might also want to look into handling unhandledRejection errors.


Here is the final solution:

'use strict';

const puppeteer = require( 'puppeteer' );

const initialPage = 'https://statsregnskapet.dfo.no/departementer';

const selectors = [
    'div[id$="-bVMpYP"] article a',
    'div[id$="-KcazEUq"] article .dfo-widget-sm a'
];

( async () =>
{
    let selector;
    let handles;
    let handle;

    const width = 1024;
    const height = 1600;

    const browser = await puppeteer.launch(
    {
        'defaultViewport' : { 'width' : width, 'height' : height }
    });

    const page = await browser.newPage();

    page.setDefaultNavigationTimeout( 90000 );

    await page.setViewport( { 'width' : width, 'height' : height } );

    await page.setUserAgent( 'UA-TEST' );

    // Load first page

    let stat = await page.goto( initialPage, { 'waitUntil' : 'domcontentloaded' } );

    // Click on selector 1 - works ok

    selector = selectors[0];
    await page.waitForSelector( selector );
    handles = await page.$$( selector );
    handle = handles[12];
    console.log( 'Clicking on: ', await page.evaluate( el => el.href, handle ) );
    await handle.click();  // OK

    await page.waitForNavigation();

    // Click that selector 2 - fails

    selector = selectors[1];
    await page.waitForSelector( selector );
    handles = await page.$$( selector );
    handle = handles[12];
    console.log( 'Clicking on: ', await page.evaluate( el => el.href, handle ) );
    await handle.click();

    await page.waitForNavigation();

    await browser.close();
})();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...