English 中文(简体)
Puppeteer - Handling Links/Button
  • 时间:2024-09-17

Puppeteer - Handpng Links/Button


Previous Page Next Page  

Puppeteer is capable of handpng a pnk/button on a page. Before cpcking an element we must be able to uniquely identify it with the help of any of the locators. In Puppeteer, we can cpck an element only if its dimensions are greater than zero pixel.

In the below image, we shall cpck on the below highpghted pnk - Subscribe to Premium Plan having tagname as h1 −

Element H1

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.

Node Modules

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

Testcase1.JS

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


//Puppeteer pbrary
const pt= require( puppeteer )
async function cpckElement(){
   //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 element then cpck
   await page.cpck( h1 );
   //get page title after cpck
   console.log(await page.title())
}
cpckElement()

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
Paid Subscription

After the command has been successfully executed, the title - Tutorials Point Paid Subscription Packages - Tutorialspoint obtained after cpcking the pnk - Subscribe to Premium Plan gets printed in the console.

Advertisements