DEV Community

Liang Wang
Liang Wang

Posted on

Playwright if locator is visible

I have a project that sometimes will throw an error when try to delete an object.
I would like to catch that error and click on the OK button to close it.

Here is what works for me.

    const element = page.getByRole("button", { name: "OK" });
    if (await element.isVisible()) {
     await element.click();
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)