- Puppeteer - Discussion
- Puppeteer - Useful Resources
- Puppeteer - Quick Guide
- Puppeteer - Capture Screenshot
- Puppeteer - Synchronization
- Puppeteer - Disable JavaScript
- Puppeteer - Device Emulation
- Puppeteer - Getting Element Attribute
- Puppeteer - Getting Element Text
- Puppeteer - Keyboard Simulation
- Puppeteer - Handling Frames
- Handling Edit Boxes & Checkboxes
- Puppeteer - Handling Links/Button
- Puppeteer - Attribute Selector
- Puppeteer - Id Selector
- Name Selector & Class Name Selector
- Puppeteer - Type Selector
- Puppeteer - Xpath Axes
- Puppeteer - Relative Xpath
- Puppeteer - Absolute Xpath
- Puppeteer - Xpath Grouping
- Puppeteer - Xpath Attributes
- Puppeteer - Xpath Functions
- Puppeteer - Locators
- Puppeteer - Handling Drop-downs
- Puppeteer - Handling Confirm Alerts
- Puppeteer - Chrome
- Puppeteer - Firefox
- Puppeteer - Basic Commands
- Puppeteer - Handling Tabs
- Puppeteer - Browser Operations
- Comparison Between Puppeteer & Cypress
- Comparison Between Puppeteer & Protractor
- Comparison Between Puppeteer & Selenium
- Puppeteer - Non Headless Execution
- Puppeteer - Basic Test
- Puppeteer - Installation
- Puppeteer VS Code Configuration
- Puppeteer - NodeJS Installation
- Puppeteer - Usage of Google
- Puppeteer - Element Handling
- Puppeteer - Introduction
- Puppeteer - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Puppeteer - Xpath Attributes
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.
However, if the id attribute is not present, we can use other attributes pke the class, name, and so on. In case the attributes pke id, name, and class are not present, we can utipse 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.
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.
The format for writing an xpath with only one attribute is as follows −
//tagname[@attribute= value ]
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 ]
The format for writing an xpath with OR condition is as follows −
//tagName[@attribute1= value1 or @attribute2= value2 ]
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 )]
Let us identify the below highpghted logo on the page with the help of the alt attribute and then cpck on it.
The xpath for the element shall be as follows −
//img[@alt= tutorialspoint ].
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.
To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer which are as follows −
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.
Right-cpck on the folder where the node_modules folder is created, then cpck on the New file button.
Step 2 − Enter a filename, say testcase1.js.
Step 3 − Add the below code within the testcase1.js file created.
//Puppeteer pbrary 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 cpck const b = (await page.$x("//img[@alt= tutorialspoint ]"))[0] b.cpck() //wait for sometime await page.waitForTimeout(4000) //obtain URL after cpck console.log(await page.url()) } selectorAttributeXpath()
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
After the command has been successfully executed, the URL of the page navigated on cpcking the logo image -
gets printed in the console. Advertisements