Puppeteer 简明教程
Puppeteer - Relative Xpath
为了唯一地确定一个元素,我们可以使用 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 等属性,我们可以使用仅对该标签提供的独特属性,或其值和属性的组合来标识一个元素。
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.
为此,我们必须使用 xpath 表达式。此外,如果页面上的元素是动态的,那么 xpath 选择器可能是一个很好的选择器选择。
For this, we have to use the xpath expression. Also, if the element on a page is dynamic, then xpath selector can be a good choice as a selector.
Relative Xpath
Xpath 可分为两种类型 - 绝对和相对。相对 xpath 从要定位的元素开始,而不是从根开始。
Xpath can be of two types - absolute and relative. A relative xpath begins from the element to be located and not from the root.
它以 // 符号开头,表示任何后代。它的优势是,即使在 DOM 中删除或添加了一个元素,特定元素的相对 xpath 也不会受到影响。
It begins with the // symbol which refers to any descendant. Its advantage is that even if an element is deleted or added in the DOM, the relative xpath for a specific element remains unaffected.
要通过属性获取相对路径,xpath 表达式如下 -
To obtain a relative path by an attribute, the xpath expression is as follows −
//tagname[@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 relative xpath for the element is given below
//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 - 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 selectorRelativeXpath(){
//launch browser in headless mode
const browser = await pt.launch()
//browser new page
const page = await browser.newPage()
//launch URL
a wait 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
//wait for sometime
await page.waitForTimeout(4000)
//obtain URL after click
console.log(await page.url())
}
selectorRelativeXpath()
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.