Puppeteer 简明教程

Puppeteer - Keyboard Simulation

Puppeteer 可以执行键盘模拟操作,如按下键盘上的键、按下向上键、向下键等等。所有这些都是使用键盘方法完成的。

Puppeteer can perform keyboard simulation actions like pressing a key in the keyboard, pressing the up, down keys, and so on. All these are done using the keyboard method.

Keyboard Methods

以下是一些键盘方法:

Some of the keyboard methods are as follows −

keyboard.press()

此方法用于模拟按键。要按下的键作为参数传递给此方法。

This method is used to simulate a key press. The key to be pressed is passed as a parameter to this method.

其语法如下:

The syntax is as follows −

Syntax

Syntax

keyboard.press('Enter')

keyboard.type()

此方法用于模拟从键盘输入文本。要输入的文本作为参数传递给此方法。

This method is used to simulate entering text from the keyboard. The text to be entered is passed as a parameter to this method.

其语法如下:

The syntax is as follows −

Syntax

Syntax

keyboard.type('Puppeteer')

keyboard.sendCharacter()

它与キーボード.类型()相同。

It is same as keyboard.type().

其语法如下:

The syntax is as follows −

Syntax

Syntax

keyboard.sendCharacter('Puppeteer')

keyboard.up()

这种方法用于模拟从键盘按下向上箭头。

This method is used to simulate pressing the up arrow from the keyboard.

其语法如下:

The syntax is as follows −

Syntax

Syntax

keyboard.up()

keyboard.down()

这种方法用于模拟从键盘按下向下箭头。

This method is used to simulate pressing the down arrow from the keyboard.

其语法如下:

The syntax is as follows −

Syntax

Syntax

keyboard.down()
keyboard down

首先,按照人偶基本测试章节执行步骤 1 至 2,如下所示 -

To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows −

Step 1 - 在创建 node_modules 文件夹的目录中创建一个新文件(人偶和人偶核已安装的位置)。

Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed).

人偶安装的详情在人偶安装篇章中进行了讨论。

The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation.

右击创建 node_modules 文件夹的文件夹,然后点击新建文件按钮。

Right-click on the folder where the node_modules folder is created, then click on the New file button.

node modules

Step 2 - 输入文件名,如 testcase1.js。

Step 2 − Enter a filename, say testcase1.js.

testcase1 js

Step 3 - 将以下代码添加到新创建的 testcase1.js 文件中。

Step 3 − Add the below code within the testcase1.js file created.

//Puppeteer library
const pt= require('puppeteer')
async function keyboradSimulation(){
   //launch browser in headless mode
   const browser = await pt.launch()
   //browser new page
   const page = await browser.newPage()
   //launch URL
   await page.goto('https://www.tutorialspoint.com/index.htm')
   //identify edit box with id
   const f = await page.$("#gsc-i-id1")
   //enter text
   f.type("Puppeteer")
   //wait for sometime
   await page.waitForTimeout(4000)
   //press Enter
   await page.keyboard.press('Enter')
   //wait for sometime
   await page.waitForTimeout(4000)
   //identify element
   const t = await page.$(".gsc-result-info")
   //obtain text
   const text = await (await t.getProperty('textContent')).jsonValue()
   console.log("Text is: " + text)
}
keyboradSimulation()

Step 4 - 使用以下命令执行代码 -

Step 4 − Execute the code with the command given below −

node <filename>

因此,在我们的示例中,我们将运行以下命令 -

So in our example, we shall run the following command −

node testcase1.js
about 39 results

在命令成功执行后,在输入 Puppeteer - About 39 results (0.15 seconds) 后按 Enter 获得的文本将打印在控制台中。

After the command has been successfully executed, the text obtained on pressing Enter after entering Puppeteer - About 39 results (0.15 seconds) gets printed in the console.