playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Cannot access element in Iframe

Open sarut0bi opened this issue 6 years ago • 7 comments

hi,

When i use following html:

<!DOCTYPE html>
<html>
<body>

<iframe src="http://the-internet.herokuapp.com/dynamic_loading/2" width="1440" height="900">
</iframe>

</body>
</html>

i cannot access, the '//button' present in the iframe.

Furthermore, When i do page.frames() only 1 frame is present (the main one i guess )...

Could you tell me a way to deal with this case ?

sarut0bi avatar Apr 16 '20 07:04 sarut0bi

What's your playwright script? Could it be that you don't wait until the iframe loads?

pavelfeldman avatar Apr 16 '20 22:04 pavelfeldman

Previously example was quite inexact... here is another working sample:

    let browser = await chromium.launch({ headless: false });
    let context = await browser.newContext();
    let page = await context.newPage();
    let temp;

    await page.goto('https://the-internet.herokuapp.com/tinymce')
    console.log('frames count:'+await page.frames().length)
    for(let frame of await page.frames())
    {
        temp = await frame.$('//button');
        if(temp)
        {
            console.log('button ok');
            break;
        }
    }    
    await temp.click();

    temp = await page.$('[id=tinymce]');
    
    for(let frame of await page.frames())
    {
        temp = await frame.$('[id=tinymce]');
        if(temp)
        {
            console.log('textarea ok');
            break;
        }
    }
    await temp.fill('');

✅ The context count is 2 ✅ click on button is also good ❌ Clear textfield seem KO ( timeout on temp = await frame.$('[id=tinymce]'); )

sarut0bi avatar Apr 17 '20 09:04 sarut0bi

fill() is for textarea. The tinymce element is a contenteditable body element. Use .textContent instead to change the text.

AutoSponge avatar May 05 '20 16:05 AutoSponge

the fact is i can't even reach fill function... A rfeeze occurs during await frame.$('[id=tinymce]'); on second frame :/

sarut0bi avatar May 06 '20 22:05 sarut0bi

I am seeing something similar when trying to click on an element within a salesforce iframe except using essentially the same code it will not even click on the button.

console.log('frames count: '+await page.frames().length);
    let temp;
    for(let frame of await page.frames())
    {
        temp = await frame.$('#cp-new-plan');
        
        if(temp){
            console.log(temp);
            break;            
        }
    }
    await temp.click();

error;

UnhandledPromiseRejectionWarning: TimeoutError: waiting for element to receive pointer events failed: timeout exceeded. Re-run with the DEBUG=pw:input env variable to see the debug log. at ElementHandle._retryPointerAction (C:\...playwright-core\lib\dom.js:194:15) (node:25772) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:25772) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

kristianwilliamssagecom avatar May 15 '20 15:05 kristianwilliamssagecom

I'll close this as a part of the bug triaging process. We have hundreds of bugs and feature requests with dozens and even hundreds of upvotes, while this one only has a few thumbs up. Please feel free to open a new bug and link this one if you'd like to see it addressed.

pavelfeldman avatar Jul 12 '21 21:07 pavelfeldman

I've also had issues with this. My workaround is to insert text via innerHTML however vanilla document.get.. doesnt exist on its own in playwright so page.evaluate is the way forward.

  const ifr = await page.evaluate(async () => {
    const iframe = document.querySelector('iframe#editMainBody_ifr') as HTMLIFrameElement;
    iframe.contentWindow!.document.body.innerHTML = 'Hello world!';
  });

Note that the iframe#editMainBody_ifr is the id of the iframe and not id of the body inside the iframe.

akwasin avatar Sep 01 '23 09:09 akwasin