Puppeteer 简明教程
Puppeteer - Element Handling
我们可以使用 Puppeteer 处理页面上的元素。一旦我们导航到一个网页,我们就需要与页面上的 web 元素交互,比如点击链接/按钮,在编辑框中输入文本,以此来完成我们的自动化测试用例。
We can handle elements on page with Puppeteer. Once we navigate to a webpage, we have to interact with the webelements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的首要任务是识别该元素。为了唯一地获取一个元素的属性,我们需要对其进行检查(右击该元素,然后选择“检查”选项)。ElementHandle 对象由方法创建,分别为:page .、page . $ $和 page . $ x。这些对象引用页面中的元素或标签。
For this, our first job is to identify the element. To get the property of an element uniquely we need to inspect it (right-click on the element then select the option Inspect). The ElementHandle objects are created by the methods - page.$, page.$$ and page.$x. These objects refer to an element or tag in a page.
Puppeteer Methods to Locate Elements
这些方法列在下面:
These methods are listed below −
page.$(locator value)
page.$(locator value)
此方法产生带有 ElementHandle 的 Promise。ElementHandle 是已识别元素的一个对象。如果有多个元素具有相同定位值,则仅从页面的左上角返回第一个匹配的元素。
This method yields a Promise with the ElementHandle. The ElementHandle is an object of the identified element. If there are multiple elements having the same locator value, then only the first matching element from the top left corner of the page shall be returned.
page.$$(locator value)
page.$$(locator value)
此方法产生带有 ElementHandle 数组的 Promise。如果有多个元素具有相同定位值,则以数组形式返回所有匹配元素。
This method yields a Promise with an array of ElementHandle. If there are multiple elements having the same locator value, then all matching elements shall be returned in the form of an array.
page.$x(xpath value)
page.$x(xpath value)
此方法产生带有 ElementHandle 数组的 Promise。如果有多个元素具有相同 xpath 值,则以数组形式返回所有匹配元素。如果有一个匹配的元素,则返回的数组将具有单个元素。
This method yields a Promise with an array of ElementHandle. If there are multiple elements having the same xpath value, then all matching elements shall be returned in the form of an array. In case, there is one matching element, then the array returned shall have a single element.
ElementHandle 方法,如 elementHandle.$、elementHandle.$$ 和 elementHandle.$x 可应用于一个元素。在这种情况下,应该在当前 ElementHandle 的 DOM 中搜索一个元素,而不在整个 DOM 中。
The ElementHandle methods like elementHandle.$, elementHandle.$$ and elementHandle.$x can be applied to an element. In that case, an element shall be searched within the DOM of the present ElementHandle and not in the entire DOM.
在下图中,让我们举一个带有 li 标签的元素(具有父元素 ul)和 class 属性值 heading 的示例。要在页面上使用 ElementHandle 方法对其进行识别,则表达式应如下所示:
In the below image, let us take the example of an element having the li tag (having a parent element ul) and class attribute value as heading. To identify it using the ElementHandle method on the page, the expression should be as follows −
const n = await page.$(".heading")
要在元素上使用 ElementHandle 方法对其进行识别,则表达式应如下所示:
To identify it using the ElementHandle method on an element, the expression should be as follows −
const m = await page.$("ul")
const p = await m.$(".heading")
现在,参阅下面给出的一个具有 li 标签的元素的图片
Now, refer the image given below of an element having the li tag

Types of Locators
以下是 Puppeteer 中的定位符类型列表:
The types of locators in Puppeteer are listed below −
-
ID
-
Class
-
Type
-
Xpath
-
Attribute
-
Type
为了使用上述定位符,我们应该对 HTML 代码有基本的了解。我们举一个具有以下特性的编辑框的示例:
To work with the above locators we should have the basic understanding of HTML code. Let us take an example of an edit box having the below mentioned properties −

此处,input 是标签名称。HTML 中的标签可能有或没有属性。元素的类型、类、名称、ID 等是元素的属性。
Here, input is the tagname. A tag in HTML may or may not have attributes. The type, class, name, id and so on are the attributes of the element.
例如,在表达式 class = "gsc-input" 中,= 左侧的文本是属性名称 (class),右侧是属性值 (gsc-input)。
For example, in the expression class = "gsc-input", text to the left of = is the attribute name (class) and to the right of = is the attribute value (gsc-input).
属性可能具有或不具有分配的值。此外,如果分配了一个值,那么它应该用双引号或单引号括起来。属性的值由开发人员根据自己的选择设置。
An attribute may or may not have a value assigned. Also, if a value is assigned, then it should be enclosed in double or single quotes. The value of an attribute is set by a developer as per his choice.