Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  38] [ 7]  / answers: 1 / hits: 37344  / 3 Years ago, wed, december 1, 2021, 12:00:00

I'm using Playwright 1.15.2 for testing and facing a problem with elements' visibility. I want to check if a modal is visible on screen so I can close it. The modal starts with display:none and turns into display:block. Also, the modal informs incorrectness in form data so it may or may not appear (i.e. I can't waitForSelector).


Currently, I have a code similar to the following:


const myModal = await page.$("#modal");
if (await myModal.isVisible()) {
await page.waitForSelector('#modal > .modal-dialog > .modal-content > .modal-footer > .btn-close');
await page.click('#modal > .modal-dialog > .modal-content > .modal-footer > .btn-close');
}

I've also tried:


const myModal = await page.$("#modal:visible");
if (myModal) {
...

Using page.$("text=modal title >> visible=true") or switching page.$ to page.locator (with all the aforementioned selectors) also didn't work.


The accepted answer for this question didn't work as well.


Could anyone help me with that?


More From » playwright

 Answers
26

page.$("text=modal title >> visible=true") does not wait until the element is on the DOM and visible.


you need to use:


await expect(page.locator("text=modal title")).toBeVisible()


see here: https://playwright.dev/docs/test-assertions#expectlocatortobevisibleoptions


[#50110] Wednesday, October 27, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gageherberth

Total Points: 249
Total Questions: 115
Total Answers: 119

Location: Liechtenstein
Member since Sun, Sep 12, 2021
3 Years ago
;