When working with Playwright for automation testing, it’s common to encounter situations where elements on a web page take time to load or appear due to asynchronous operations like API calls or animations. Interacting with elements before they are visible can lead to flaky or failed tests. To ensure your scripts are reliable and stable, it’s important to wait for elements to become visible before performing actions on them.
In this guide, we’ll explore the different ways to wait for an element to be visible in Playwright, such as the waitForSelector(), waitFor(), and toBeVisible() methods.
Wait for the element to be visible using the waitForSelector() method
The most basic and straightforward way to wait for an element to become visible in Playwright is by using the waitForSelector() method. When used with the state: ‘visible’ option, this method waits for the element to be present in the DOM and visible on the page. Additionally, you can specify a custom timeout to control how long Playwright should wait before throwing an error if the element doesn’t appear.
Let’s take a look at how to wait for an element using the waitForSelector() method in Playwright with a simple example.
Example: Wait for the element using waitForSelector() method
const { test, expect } = require('@playwright/test');
test('Wait for element to be visible in Playwright Using waitForSelector() method.', async ({ page }) => {
await page.goto('http://only-testing-blog.blogspot.com/2025/04/alert-dialogs.html');
// Wait for an element to be visible using waitForSelector with the 'visible' state and timeout.
try {
await page.waitForSelector('#nativeDate', { state: 'visible', timeout: 5000});
console.log('Element is visible! Now, you can iteract with it.');
} catch (error) {
console.log('Element not found within 5 seconds.');
}
});

Code Breakdown
try {
- Begins a try block, which allows you to handle any errors that might occur during the execution of the code inside it.
await page.waitForSelector('#nativeDate', { state: 'visible', timeout: 5000 });
- This line tells Playwright to wait for an element with the selector #nativeDate:
- state: ‘visible’ ensures the element is not only present in the DOM but also visible on the page.
- timeout: 5000 sets the maximum waiting time to 5 seconds. If the element doesn’t become visible within this time, an error is thrown.
console.log('Element is visible! Now, you can interact with it.');
- If the element becomes visible within the timeout, this message is printed, indicating it’s safe to interact with the element (e.g., click or type).
} catch (error) {
- If any error occurs inside the try block (such as the element not becoming visible in time), execution jumps to the catch block, and the error is caught here.
console.log('Element not found within 5 seconds.');
- This message is printed if the element wasn’t found or didn’t become visible within 5 seconds, helping you handle the failure gracefully (e.g., retry, log, or exit).
Wait for the element present using the waitFor() method
Another way to wait for an element on the page in Playwright is by using the locator().waitFor() method. This method can also be used with the visible state and a custom timeout. It waits for the element to meet the specified condition within the defined time period, making it useful for ensuring the element is ready before interacting with it.
Let’s see how to use the waitFor() method to wait for an element to be visible on the page in Playwright:
Example: Wait for an element using the waitFor() method
const { test, expect } = require('@playwright/test');
test('Wait for element to be visible in Playwright Using waitFor() method.', async ({ page }) => {
await page.goto('http://only-testing-blog.blogspot.com/2025/04/alert-dialogs.html');
// Wait for an element to be visible using waitFor() with the 'visible' state
try {
await page.locator('#nativeDate').waitFor({ state: 'visible', timeout: 5000});
console.log('Element exists!');
} catch (error) {
console.log('Element not found within 5 seconds.');
}
});

Code Breakdown
- page.locator(‘#nativeDate’): Creates a locator for the element with the CSS selector #nativeDate. This is more efficient than waitForSelector() and recommended in modern Playwright scripts.
- .waitFor({ state: ‘visible’, timeout: 5000 }): Instructs Playwright to wait until:
- The element is attached to the DOM and visible (i.e., not hidden or transparent).
- The maximum wait time is 5 seconds (5000 ms). If the element doesn’t meet the condition in this time, an error is thrown.
Wait for the element to be visible using the toBeVisible() Assertion
In Playwright browser automation testing, the toBeVisible() assertion is used to ensure that an element becomes visible on the page. This method waits up to 5 seconds for the element to appear, making it useful for handling dynamic content during automated UI testing.
Let’s look at a practical example to better understand how the toBeVisible() assertion works in Playwright. This will demonstrate how you can wait for an element to become visible during automated browser testing.
Playwright Example: Wait for Element to Be Visible Using toBeVisible() Assertion
const { test, expect } = require('@playwright/test');
test('Wait for element to be visible in Playwright Using toBeVisible() assertion.', async ({ page }) => {
await page.goto('http://only-testing-blog.blogspot.com/2025/04/alert-dialogs.html');
// Wait for an element to be visible using toBeVisible() assertion.
try {
await expect(page.locator('#nativeDate')).toBeVisible();
console.log('Element exists!');
} catch (error) {
console.log('Element not found within default timeout 5 seconds');
}
});

Code Breakdown
- page.locator(‘#nativeDate’): This targets the element with the ID nativeDate on the page.
- expect(…).toBeVisible(): This assertion waits for the element to be visible on the page.
- Default timeout: Playwright waits up to 5 seconds by default for the element to appear and become visible.
- If the element becomes visible on the page within 5 seconds, Playwright will log the message ‘Element exists!’ to the console.
- If the element does not become visible on the page within 5 seconds, Playwright will log the message ‘Element not found within default timeout 5 seconds’ to the console.
Final Thoughts
Learning how to wait for an element to be present in Playwright is essential for reliable browser automation. In this article, we explored three different methods to wait for an element to become visible on the page: waitForSelector(), waitFor(), and toBeVisible(). Each method was demonstrated with practical examples to help you understand how they work in real-world scenarios. You can choose any of these methods based on your specific test requirements.
Related Articles
- How to Verify if an Element Exists in Playwright: 4 Ways
- How to Maximize Browser Window in Playwright
- How to Scroll in Playwright (Down and Top)
Hi, I’m Aravind, a seasoned Automation Test Engineer with 17+ years of industry experience. I specialize in tools like Selenium, JMeter, Appium, and Excel automation. Through this blog, I share practical tutorials, tips, and real-world insights to help testers and developers sharpen their automation skills.