Rayrun
← Back to Discord Forum

A script with iframe works fine in headed mode but fails with error 'target:closed' on headless mode

Please check the simple code that I have written where the intention is to find a locator on an iframe in the page, click on it, wait for it to navigate to the page, and extract a value. It works as expected when ran on headed mode for any browser. For some strange reason, when I change the mode to headless, it fails with 'locator.click: Target closed' error. :

const {test,expect} = require('@playwright/test');

test('Validate hidden and visible elements', async({browser})=> { const context = await browser.newContext(); const page = await context.newPage(); const url = 'https://rahulshettyacademy.com/AutomationPractice/';

await page.goto(url);
const iFramePage = page.frameLocator('#courses-iframe');
await iFramePage.locator("li a[href*='lifetime-access']").first().click();
const subscriberCount = await iFramePage.locator('div h2 span').last().textContent();
console.log(subscriberCount);

}); I've reinstalled playwright yesterday and therefore it is the latest stable version. I am using intel mac pro 2018. OS - ventura 13.4 Can someone please take a look and help me to understand what am I doing wrong or is this a known issue or something? I find it quite strange. Do let me know if you'd require more details.

image.png
image.png

This thread is trying to answer question "What is causing the 'target:closed' error in headless mode and how can it be fixed?"

0 replies
TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].