操纵up按钮 [英] Puppeteer Button Press
问题描述
根据 https://github.com /GoogleChrome/puppeteer/blob/master/docs/api.md#pagepresskey-options ,您可以使用Puppeteer模拟键盘按钮的按下.
According to https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepresskey-options, you can simulate the pressing of a keyboard button with Puppeteer.
这就是我要做的:
// First, click the search button
await page.click('#outer-container > nav > span.right > span.search-notification-wrapper > span > form > input[type="text"]');
// Focus on the input field
await page.focus('#outer-container > nav > span.right > span.search-notification-wrapper > span > form > input[type="text"]');
// Enter some text into the input field
await page.type("Bla Bla");
// Press Enter to search -> this doesn't work!
await page.press("Enter");
按下按钮不会产生任何结果.基本上被忽略了.
The pressing of the button doesn't produce anything. It's basically ignored.
如何模拟Enter键提交表单?
How can I simulate the Enter key to submit the form?
推荐答案
我终于知道了.我在同一个表单中发现了一个类型为Submit的锚元素.然后,我单击它并提交了表单.
I've figured it out finally. I found inside that same form an anchor element whose type was submit. I then clicked on it and the form was submitted.
这是我使用的代码:
const form = await page.$('a#topbar-search');
await form.evaluate( form => form.click() );
您也可以使用$ eval方法代替评估:
You can also use the $eval method instead of evaluate:
await page.$eval( 'a#topbar-search', form => form.click() );
这篇关于操纵up按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!