Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 木偶手按钮按下_Javascript_Node.js_Google Chrome Devtools_Puppeteer_Headless Browser - Fatal编程技术网

Javascript 木偶手按钮按下

Javascript 木偶手按钮按下,javascript,node.js,google-chrome-devtools,puppeteer,headless-browser,Javascript,Node.js,Google Chrome Devtools,Puppeteer,Headless Browser,根据,您可以使用木偶演员模拟按下键盘按钮 我是这样做的: // 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.foc

根据,您可以使用木偶演员模拟按下键盘按钮

我是这样做的:

// 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");
按下按钮不会产生任何效果。它基本上被忽略了


如何模拟Enter键来提交表单?

我终于找到了答案。我在同一个表单中发现了一个类型为submit的锚元素。然后我点击它,表格就提交了

以下是我使用的代码:

const form = await page.$('a#topbar-search');
await form.evaluate( form => form.click() );
您也可以使用$eval方法代替evaluate:

await page.$eval( 'a#topbar-search', form => form.click() );

也许另一个选择是:

await page.keyboard.press('Enter');

利用其虚拟键盘API。更多信息可以使用xpath创建自己的元素

const browser = await puppeteer.launch();
const page = await browser.newPage();
const txtObject = await page.$x('.//input[type="text"]');
await txtObject[0].type('bla bla bla');
在上面的代码中,我们使用“$x”检索一个数组中的所有元素,这就是为什么我们在下一代码行中使用“0”作为索引。如果有多个对象,可以使用“如果”条件和一个循环来搜索正确的空间并使用正确的对象。另一种方法是,如果你有一些属性,你也可以使用类似的东西来有一个干净和简单的方法来识别你的对象

await page.click('input[type="submit"]'); // With type
await page.click('input[class="ng-newstyle"]'); //With class attribute
您可以在不久的将来降低维护的复杂性

await page.$eval('input[name=btnK]', el => el.click());

这将单击谷歌中的提交按钮。至于您的页面,请找出元素或按钮id,然后使用它。

在撰写本文时,
page.press()
不是有效的木偶制作方法。使用
page.press()
会导致以下错误:

运行代码时出错。TypeError:page.press不是一个函数

你可能指的是

有关如何在Puppeter中模拟Enter键的详细信息,请参见下面的列表:


页面。键盘。按() 可以使用模拟按enter键。以下任何选项都应起作用:

await page.keyboard.press('Enter'); // Enter Key
await page.keyboard.press('NumpadEnter'); // Numeric Keypad Enter Key
await page.keyboard.press('\n'); // Shortcut for Enter Key
await page.keyboard.press('\r'); // Shortcut for Enter Key
elementHandle.press(): 此外,在按enter键之前,可以使用和的组合来聚焦图元:

await (await page.$('input[type="text"]')).press('Enter'); // Enter Key
await (await page.$('input[type="text"]')).press('NumpadEnter'); // Numeric Keypad Enter Key
await (await page.$('input[type="text"]')).press('\n'); // Shortcut for Enter Key
await (await page.$('input[type="text"]')).press('\r'); // Shortcut for Enter Key
page.type(): 此外,您可以使用:

page.keyboard.type(): 同样,您可以使用:

page.keyboard.sendCharacter(): 另一种替代方法是使用以下方法:

page.keyboard.down()/page.keyboard.up(): 您还可以使用和的组合:


您可以使用下面的代码点击按钮

const searchButtonNodeSelector=“.submit按钮”;
等待页面。单击(搜索按钮节点选择器);

此API已解释。

可能尝试提交表单
const form=wait page.$(“#外部容器>导航>span.right>span.search-notification-wrapper>span>form”);等待form.evaluate(form=>form.submit())
会试试的,谢谢@codtex!我试过了。它似乎做了些什么(结果是页面基本上重新加载)。但是,所需的表格没有提交,我也看不到任何结果。对不起,Elena我不是木偶专家,这是我第一次检查它的文档,但是你任务的目的是什么-你想实际提交表格吗?或者找到并单击提交按钮?-可能有一个javascript正在执行搜索而不重新加载page@codtex当然,我明白。嗯,我只是想得到搜索结果。有一个搜索输入框,您可以在其中输入搜索词。当按下“回车”按钮时,应返回结果。谢谢分享!这是一种糟糕的做法,您应该返回元素,然后在下一行代码中使用puppeter click函数单击它
await page.type(String.fromCharCode(13));
await page.keyboard.type(String.fromCharCode(13));
await page.keyboard.sendCharacter(String.fromCharCode(13));
// Enter Key
await page.keyboard.down('Enter');
await page.keyboard.up('Enter');

// Shortcut for Enter Key
await page.keyboard.down('NumpadEnter');
await page.keyboard.up('NumpadEnter');

// Shortcut for Enter Key
await page.keyboard.down('\n');
await page.keyboard.up('\n');

// Shortcut for Enter Key
await page.keyboard.down('\r');
await page.keyboard.up('\r');