Handling Dropdowns in 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:
Handling Dropdowns in Playwright (JavaScript/TypeScript)
Dropdowns are a common web element, and automating them in Playwright is both simple and powerful. Whether it's a single-select or multi-select dropdown, Playwright provides efficient methods to interact with them.
🔹 1. Handling Single-Select Dropdowns (Using selectOption)
javascript
Copy
Edit
await page.selectOption('#dropdownId', 'optionValue');
✅ You can also select by label or index:
javascript
Copy
Edit
await page.selectOption('#dropdownId', { label: 'Option 1' });
await page.selectOption('#dropdownId', { index: 2 });
🔹 2. Handling Multi-Select Dropdowns
javascript
Copy
Edit
await page.selectOption('#multiSelectId', [
{ value: 'value1' },
{ value: 'value2' }
]);
This selects multiple values at once. Make sure the dropdown supports multi-select.
🔹 3. Get Selected Option
javascript
Copy
Edit
const selectedValue = await page.$eval('#dropdownId', el => el.value);
console.log(selectedValue);
🔹 4. Using click for Custom Dropdowns (Non-HTML select)
Some websites use stylized dropdowns that don’t use <select> tags. You need to click and choose manually:
javascript
Copy
Edit
await page.click('.dropdown-toggle'); // Open dropdown
await page.click('text=Option Name'); // Select item by visible text
Read more:
Working with Checkboxes and Radio Buttons
What are the advantages of using Playwright in 2025?
Taking Screenshots with Playwright
Extracting Text from Web Pages
Comments
Post a Comment