Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking inside an iframe? #553

Open
BeatBroccoli opened this issue Mar 2, 2024 · 1 comment
Open

Clicking inside an iframe? #553

BeatBroccoli opened this issue Mar 2, 2024 · 1 comment

Comments

@BeatBroccoli
Copy link

Environment

Component Version
Node.js v20.11.1
Client (Chrome/Chromium/...) Google Chrome 122
OS running Node.js Windows 11
OS running the client Windows 11
chrome-remote-interface 0.33.0

Is the client running in a container? NO

Description

Hi, sometimes i cant evaluate scripts inside an iframe. The context doesnt show up.
If I open devtools i cant cleary see there is an context but the protocol doesnt provide access to it, at least not via
Runtime.executionContextCreated

Example

import * as chromeLauncher from 'chrome-launcher';
import CDP, * as cri from 'chrome-remote-interface';

(async () => {

    let chrome = await chromeLauncher.launch({ port: 9500 });
    let client = await cri.default({ port: chrome.port });

    const { Runtime, Page, Target } = client;
    await Promise.all([Page.enable(), Runtime.enable()]);
    await Target.setAutoAttach({ autoAttach: true, waitForDebuggerOnStart: false });

    Runtime.executionContextCreated(async (evt) => {
        await Runtime.callFunctionOn({
            executionContextId: evt.context.id,
            functionDeclaration: 'function() { alert(document.location.href); let $box = document.querySelector("input"); if($box) $box.click(); return $box; }'
        });
    });

    await Target.targetCreated(({ targetInfo }) => { console.log(`New target created: ${targetInfo.url}`); });
    await Page.navigate({ url: 'https://nopecha.com/demo/cloudflare' });

})();
@joeledwardson
Copy link

Few things to try:

  • Call Target.setDiscoverTargets with dicover as true initially
  • When targets are created you will be a given a sessionId to interact with, you need to listen to Target.attachedToTarget events to get these, which should be fired due to setAutoAttach. (In my experience this doesn't always work with iframe targets, you might need to call attachToTarget explicitly)
  • You should call Runtime.enable with any new sessionIds from targets to ensure executionContextCreated is fired from within that session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants