'Need to fetch text of a span class inside iframe in playwright javascript

Image of HTML structure

Can someone explain me how to get the text of the start game button which is present inside iFrame in Playwright JavaScript ,

I tried using below code but that is not working for me

        console.log(frame)
        // const text= await frame.locator("//div[text()='Start Game']").textContent();
        // console.log(text);```


Solution 1:[1]

Use frameLocator function to access the content within iFrame.

const btnStart = page.frameLocator('#external-app').locator('text=Start Game');
await btnStart.click();

More details: https://playwright.dev/docs/api/class-framelocator

To access the value or text, you can use page.innerText() or page.inputValue().

More infos here: https://playwright.dev/docs/api/class-page#page-input-value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r00k13