问题描述
我想单击 html 页面中的链接,其中包含以下代码段:
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>
我之前设置了一些过滤器,然后我正在加载页面,这将加载我需要的页面.在结果页面上,我想单击 html 代码段中的链接.我正在尝试使用的 js 是这个
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
我很确定 page.click('p a')
工作正常,因为在我的 chrome 浏览器的控制台中,我可以执行 document.querySelector("pa").click()
,然后按预期重新加载页面.
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.
我还尝试使用 href attr 来选择网址,例如使用 page.click('a[href="/rp_web/search.do?doppelt"]')
,但出现错误:没有找到选择器的节点:a[href="/rp_web/search.do?doppelt"]
.
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 accomplish what I expect to happen?
编辑您可以在此处找到完整的存储库:bitbucket/ytNeskews
EDIT You can find the complete repo here: bitbucket/ytNeskews
推荐答案
有很多关于 page.click
不工作的报告,在您的情况下,由于某种原因它确实不会工作.幸运的是,我们可以在旧的 page.evaluate
(或 page.$eval
)的帮助下完成所有操作:这里我在浏览器上下文中手动单击链接:
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();
})();
结果:
200 次点击
400 次点击
也不是说您应该一次只等待一个页面导航.如果可以的话,还有一个注意事项 - 在 Chromium 可见 ({headless : false}) 的情况下编写此类脚本要方便得多.
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}).
这篇关于Puppeteer 选择链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!