最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer Select Link - Stack Overflow

programmeradmin5浏览0评论

I want to click on a link in a html page, which contains following snippet:

<p>Die maximale Trefferanzahl von 200 wurde überschritten.
  <a href="/rp_web/search.do?doppelt">Verdoppeln Sie hier  Suchergebnislimit.</a>
</p>

I'm setting some filters before and then I am loading the page, which loads the page I need. On that resulting page, I want to click on the link as seen in the html snippet. The js I'm trying to use is this one

await Promise.all([
    page.click('input#landNW'), // set a filter
    page.click('input[type=submit]'), // submit the form
    page.waitForNavigation(), // wait for the page to load
    page.click('p a'), // not working: double the search results
    page.waitForNavigation() // not working: waiting for the page to reload
]).catch(e => console.log(e)); // no error

I am pretty sure the page.click('p a') is working properly, because in the console of my chrome browser I can do document.querySelector("p a").click(), which then reloads the page as expected.

I have also tried to select the url by using the href attr, e.g. with page.click('a[href="/rp_web/search.do?doppelt"]'), but I got an error: No node found for selector: a[href="/rp_web/search.do?doppelt"].

How can I acplish what I expect to happen?

EDIT You can find the plete repo here: bitbucket/ytNeskews

I want to click on a link in a html page, which contains following snippet:

<p>Die maximale Trefferanzahl von 200 wurde überschritten.
  <a href="/rp_web/search.do?doppelt">Verdoppeln Sie hier  Suchergebnislimit.</a>
</p>

I'm setting some filters before and then I am loading the page, which loads the page I need. On that resulting page, I want to click on the link as seen in the html snippet. The js I'm trying to use is this one

await Promise.all([
    page.click('input#landNW'), // set a filter
    page.click('input[type=submit]'), // submit the form
    page.waitForNavigation(), // wait for the page to load
    page.click('p a'), // not working: double the search results
    page.waitForNavigation() // not working: waiting for the page to reload
]).catch(e => console.log(e)); // no error

I am pretty sure the page.click('p a') is working properly, because in the console of my chrome browser I can do document.querySelector("p a").click(), which then reloads the page as expected.

I have also tried to select the url by using the href attr, e.g. with page.click('a[href="/rp_web/search.do?doppelt"]'), but I got an error: No node found for selector: a[href="/rp_web/search.do?doppelt"].

How can I acplish what I expect to happen?

EDIT You can find the plete repo here: bitbucket/ytNeskews

Share Improve this question edited Jun 25, 2018 at 5:30 Vaviloff 16.8k6 gold badges53 silver badges62 bronze badges asked Jun 24, 2018 at 15:43 NeskewsNeskews 8241 gold badge11 silver badges23 bronze badges 2
  • Great question — clear problem statement, code sample included — it does show effort made! – Vaviloff Commented Jun 25, 2018 at 5:43
  • And an answer as clear and informative as yours is always pleasant! – Neskews Commented Jun 25, 2018 at 13:27
Add a ment  | 

2 Answers 2

Reset to default 10

There are lots of reports about page.click not working and in your case it indeed won't work for some reason. Luckily we can do everything with the help of a good old page.evaluate (or page.$eval): here I'm clicking the link manually in the browser context:

const puppeteer  = require ('puppeteer');
(async () => {
    const browser = await puppeteer.launch({ headless : false });
    const page = await browser.newPage();
    await page.goto('https://www.handelsregister.de/rp_web/mask.do?Typ=e');

    await Promise.all([
        page.click('input#landNW'), // set a filter
        page.click('input[type=submit]'), // submit the form
        page.waitForNavigation(), // wait for the page to load
    ]).catch(e => console.log(e));

    // Print the number of allowed results (must be 200)
    console.log(await page.$eval('#inhalt p', el => el.textContent.match(/\d+ hits/)[0]));

    await Promise.all([
         // Manual clicking of the link
         page.$eval('p a', el => el.click()),
         page.waitForNavigation()
    ]).catch(e => console.log(e));

    // Print the number of allowed results (must be 400 now)
    console.log(await page.$eval('#inhalt p', el => el.textContent.match(/\d+ hits/)[0]));

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

Results:

200 hits
400 hits

Also not that you should wait only for one page navigation at once. And one more note if I may — it is much more convenient to write such scripts with Chromium visible ({headless : false}).

The code seems fine and I think puppeteer is actually attempting the click. However, it's not clicking the link in question.

Change the viewport by

await page.setViewport({width: 1366, height: 768})

and your code seems to work. Have notified the puppeteer team about this possible bug.

发布评论

评论列表(0)

  1. 暂无评论