Puppeteer 简明教程
Puppeteer - Xpath Attributes
为了唯一地确定一个元素,我们可以使用 html 标签内的任何属性或使用 html 标签上的多个属性的组合。通常使用 id 属性,因为它对于页面是唯一的。
To determine an element uniquely, we can either take the help of any of the attributes within the html tag or we can use a combination of attributes on the html tag. Mostly the id attribute is used since it is unique to a page.
但是,如果 id 属性不存在,我们可以使用其他属性,例如 class、name 等。如果 id、name 和 class 等属性不存在,我们可以利用仅该标签专有的不同属性或属性及其值的组合来识别元素。为此,我们必须使用 xpath 表达式。
However, if the id attribute is not present, we can use other attributes like the class, name, and so on. In case the attributes like id, name, and class are not present, we can utilise a distinct attribute available to only that tag or a combination of attributes and their values to identify an element. For this, we have to use the xpath expression.
如果带有单个属性的 xpath 表达式标识多个元素,我们可以使用 xpath 表达式中的多个属性来查找单个元素。
If an xpath expression with a single attribute identifies multiple elements, we can use more than one attribute in the xpath expression to locate a single element.
仅用一个属性编写 xpath 的格式如下−
The format for writing an xpath with only one attribute is as follows −
//tagname[@attribute='value']
对于多个属性,我们可以应用 AND 和 OR 条件。带有 AND 条件编写 xpath 的格式 −
For multiple attributes, we can apply AND and OR conditions. The format for writing an xpath with AND condition −
//tagName[@attribute1='value1'] [@attribute2='value2']
或者,
Or,
//tagName[@attribute1='value1' and @attribute2='value2']
带有 OR 条件编写 xpath 的格式如下−
The format for writing an xpath with OR condition is as follows −
//tagName[@attribute1='value1' or @attribute2='value2']
我们还可以通过对属性应用 NOT 条件来识别元素。带有 NOT 条件编写 xpath 的格式 −
We can also identify an element by applying the NOT condition on an attribute. The format for writing an xpath with NOT condition −
//tagname[not(@attribute='value')]
让我们借助 alt 属性识别页面上下面突出显示的徽标,然后单击它。
Let us identify the below highlighted logo on the page with the help of the alt attribute and then click on it.

元素的 xpath 如下−
The xpath for the element shall be as follows −
//img[@alt='tutorialspoint'].
在这里,我们正在使用 xpath 选择器,因此我们必须使用以下方法:page.$x(xpath 值)。有关此方法的详细信息在 Puppeteer 定位器章节中讨论。
Here, we are working with the xpath selector, so we have to use the method: page.$x(xpath value). The detail on this method is discussed in the Chapter of Puppeteer Locators.
首先,按照人偶基本测试章节执行步骤 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.

Step 2 - 输入文件名,如 testcase1.js。
Step 2 − Enter a filename, say 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 selectorAttributeXpath(){
//launch browser in headed mode
const browser = await pt.launch()
//browser new page
const page = await browser.newPage()
//launch URL
await page.goto('https://www.tutorialspoint.com/questions/index.php')
//identify element with relative xpath then click
const b = (await page.$x("//img[@alt='tutorialspoint']"))[0]
b.click()
//wait for sometime
await page.waitForTimeout(4000)
//obtain URL after click
console.log(await page.url())
}
selectorAttributeXpath()
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

在成功执行命令后,单击徽标图像时导航到的页面的 URL - https://www.tutorialspoint.com/index.htm 将打印在控制台中。
After the command has been successfully executed, the URL of the page navigated on clicking the logo image - https://www.tutorialspoint.com/index.htm gets printed in the console.