木偶师选择链接



我想点击html页面中的一个链接,其中包含以下代码片段:

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

我之前设置了一些过滤器,然后我正在加载页面,这会加载我需要的页面。在该生成的页面上,我想单击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(),然后按预期重新加载页面。

我还尝试使用 href attr 选择网址,例如使用page.click('a[href="/rp_web/search.do?doppelt"]'),但我得到一个错误:No node found for selector: a[href="/rp_web/search.do?doppelt"].

我怎样才能完成我期望发生的事情?

编辑你可以在这里找到完整的存储库:bitbucket/ytNeskews

有很多关于page.click不起作用的报告,在您的情况下,由于某种原因它确实不起作用。幸运的是,我们可以在一个好的旧page.evaluate(或page.$eval(的帮助下做所有事情:在这里,我在浏览器上下文中手动单击链接:

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点击 400点击

也不是说您应该一次只等待一个页面导航。如果可以的话,还有一点说明 - 在可见的 Chromium 下编写这样的脚本要方便得多({无头:假}(。

代码看起来很好,我认为木偶师实际上是在尝试点击。但是,它没有单击有问题的链接。

将视口更改为

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

而且您的代码似乎有效。已将此可能的错误通知了木偶师团队。

最新更新