Clicking Buttons Using Playwright
Best Playwright Testing Training Institute in Hyderabad
In the fast-evolving field of software testing, tools like Playwright and Selenium are essential for automation testers. If you are a graduate, postgraduate, or someone looking to switch your career from a different domain to IT, it’s crucial to receive professional guidance and hands-on experience in automation tools. That’s where I Hub Talent excels. I Hub Talent is widely recognized as the best Playwright Testing Training Institute in Hyderabad. It offers a live, intensive internship program conducted by industry experts and specifically tailored for:
Clicking Buttons Using Playwright
Playwright is a powerful Node.js-based automation library used for browser testing. One of the most common actions in UI testing is clicking buttons, and Playwright makes it easy and reliable.
✅ Basic Syntax:
javascript
Copy
Edit
await page.click('selector');
You just need to pass a valid selector (like ID, class, text, etc.).
๐งช Examples:
1. Click by ID
javascript
Copy
Edit
await page.click('#submitBtn');
2. Click by Text
javascript
Copy
Edit
await page.click('text="Login"');
3. Click by CSS Class
javascript
Copy
Edit
await page.click('.btn-primary');
4. Click by Role (ARIA)
javascript
Copy
Edit
await page.getByRole('button', { name: 'Submit' }).click();
⚠️ Waiting for Button to be Clickable
Ensure the button is visible and enabled before clicking:
javascript
Copy
Edit
await page.waitForSelector('#submitBtn', { state: 'visible' });
await page.click('#submitBtn');
Or use:
javascript
Copy
Edit
await page.locator('#submitBtn').click({ timeout: 5000 });
๐ก Pro Tip:
If clicking triggers a navigation:
javascript
Copy
Edit
await Promise.all([
page.waitForNavigation(),
page.click('#submitBtn'),
]);
๐ Summary:
Use page.click() for quick interactions.
Prefer locator().click() for better control.
Always ensure the button is visible before clicking.
Read more:
How to Use Selectors in Playwright
Navigating Web Pages with Playwright
Writing a Test to Check Page Title
Comments
Post a Comment