木偶师 Select Link
Puppeteer Select Link
我想点击 html 页面中的 link,其中包含以下代码段:
<p>Die maximale Trefferanzahl von 200 wurde überschritten.
<a href="/rp_web/search.do?doppelt">Verdoppeln Sie hier Suchergebnislimit.</a>
</p>
我之前设置了一些过滤器,然后加载页面,它加载了我需要的页面。在生成的页面上,我想单击 link,如 html 片段中所示。
我正在尝试使用的 js 是这个
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
我很确定 page.click('p a')
工作正常,因为在我的 chrome 浏览器的控制台中我可以执行 document.querySelector("p a").click()
,然后按预期重新加载页面。
我也尝试过 select url 通过使用 href 属性,例如使用 page.click('a[href="/rp_web/search.do?doppelt"]')
,但出现错误:
No node found for selector: a[href="/rp_web/search.do?doppelt"]
.
我怎样才能完成我期望发生的事情?
编辑 你可以在这里找到完整的 repo:bitbucket/ytNeskews
有很多关于 page.click
不起作用的报告,在您的情况下,它确实由于某种原因不起作用。幸运的是,我们可以在老 page.evaluate
(或 page.$eval
)的帮助下完成所有事情:在这里,我在浏览器上下文中手动单击 link:
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();
})();
结果:
200 hits
400 hits
也不是说您应该一次只等待一个页面导航。如果可以的话,再多说一句——用 Chromium visible ({headless : false}) 编写这样的脚本要方便得多。
代码看起来不错,我认为 puppeteer 实际上是在尝试点击。但是,它没有单击有问题的 link。
将视口更改为
await page.setViewport({width: 1366, height: 768})
并且您的代码似乎有效。已将此可能的错误通知人偶团队。
我想点击 html 页面中的 link,其中包含以下代码段:
<p>Die maximale Trefferanzahl von 200 wurde überschritten.
<a href="/rp_web/search.do?doppelt">Verdoppeln Sie hier Suchergebnislimit.</a>
</p>
我之前设置了一些过滤器,然后加载页面,它加载了我需要的页面。在生成的页面上,我想单击 link,如 html 片段中所示。 我正在尝试使用的 js 是这个
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
我很确定 page.click('p a')
工作正常,因为在我的 chrome 浏览器的控制台中我可以执行 document.querySelector("p a").click()
,然后按预期重新加载页面。
我也尝试过 select url 通过使用 href 属性,例如使用 page.click('a[href="/rp_web/search.do?doppelt"]')
,但出现错误:
No node found for selector: a[href="/rp_web/search.do?doppelt"]
.
我怎样才能完成我期望发生的事情?
编辑 你可以在这里找到完整的 repo:bitbucket/ytNeskews
有很多关于 page.click
不起作用的报告,在您的情况下,它确实由于某种原因不起作用。幸运的是,我们可以在老 page.evaluate
(或 page.$eval
)的帮助下完成所有事情:在这里,我在浏览器上下文中手动单击 link:
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();
})();
结果:
200 hits
400 hits
也不是说您应该一次只等待一个页面导航。如果可以的话,再多说一句——用 Chromium visible ({headless : false}) 编写这样的脚本要方便得多。
代码看起来不错,我认为 puppeteer 实际上是在尝试点击。但是,它没有单击有问题的 link。
将视口更改为
await page.setViewport({width: 1366, height: 768})
并且您的代码似乎有效。已将此可能的错误通知人偶团队。