How to interact with popup windows using the headless chrome?

1,003 views
Skip to first unread message

xanato...@gmail.com

unread,
May 21, 2017, 9:26:22 AM5/21/17
to headless-dev
How can I interact using the headless chrome with popups the page I'm "playing with" opens?
I tried
Target.targetCreated((params) => {
console.log("Target Created");
});
but that does not do anything when the popup opens :(


What I would like to do is, when the popup opens to be able to interact with it the same way as I do with the page i loaded with
Page.navigate({url: url});
that is see all the loaded resources, see the actual page url and be able to interact with it.

Any help woudl be greatly appriciated :-)

Anton Bacaj

unread,
May 21, 2017, 9:39:00 AM5/21/17
to xanato...@gmail.com, headless-dev

--
You received this message because you are subscribed to the Google Groups "headless-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to headless-dev+unsubscribe@chromium.org.
To post to this group, send email to headle...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/headless-dev/07750c89-89e1-46af-96c7-43ef2a18d503%40chromium.org.

xanato...@gmail.com

unread,
May 21, 2017, 10:09:22 AM5/21/17
to headless-dev, xanato...@gmail.com
Ok, settingĀ  Target.setDiscoverTargets({discover: true});
helped with that, but next problem:

Target.targetCreated((params) => {
console.log("Target Created");
    if(params.targetInfo.type != "other")
return;
console.log("Target Attaching");
Target.attachedToTarget({targetInfo: params.targetInfo, waitingForDebugger: false})
});

attache to target fails:
TypeError: "listener" argument must be a function
at _addListener (events.js:216:11)
at Chrome.addListener (events.js:276:10)
at Object.handler [as attachedToTarget] (\node_modules\chrome-remote-interface\lib\api.js:40:16)
at Chrome.inspector.inspectPage.CDP.then.Target.targetCreated (\inspector.ts:169:24)
at emitOne (events.js:96:13)
at Chrome.emit (events.js:188:7)
at Chrome.handleMessage (\node_modules\chrome-remote-interface\lib\chrome.js:237:16)
at WebSocket.<anonymous> (\node_modules\chrome-remote-interface\lib\chrome.js:200:27)
at emitTwo (events.js:106:13)
at WebSocket.emit (events.js:191:7)

xanato...@gmail.com

unread,
May 21, 2017, 10:19:26 AM5/21/17
to headless-dev, xanato...@gmail.com
ah...
Target.attachedToTarget
should be
Target.attachToTarget
now it seams to work
thanks

xanato...@gmail.com

unread,
May 21, 2017, 10:32:55 AM5/21/17
to headless-dev, xanato...@gmail.com
CDP(chromeOptions).then((client) => {
// extract domains
const {Network, Page, Target} = client;

Target.setDiscoverTargets({discover: true});


Target.targetCreated((params) => {
console.log("Target Created");
        if(params.targetInfo.type != "other")
return;
console.log("Target Attaching");
        Target.attachToTarget({targetId: params.targetInfo.targetId}).then((test)=>{
debugger;
})
});

Target.targetDestroyed((params) => {
console.log("Target Destroyed");
});



now it says its attached, but i don't see any resources of the new windows being loaded in
Network.responseReceived

do I need a new instance of the debugger to work?

Anton Bacaj

unread,
May 21, 2017, 10:49:39 AM5/21/17
to xanato...@gmail.com, headless-dev
I might be wrong but, once you do attachToTarget, try using `Page` (it will have the new target).

--
You received this message because you are subscribed to the Google Groups "headless-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to headless-dev+unsubscribe@chromium.org.
To post to this group, send email to headle...@chromium.org.

xanato...@gmail.com

unread,
May 21, 2017, 11:13:27 AM5/21/17
to headless-dev, xanato...@gmail.com
i dont know about page but Network should show the traffic for teh new tab but it does not do that :/
i think i do something wrong when attaching to the new tab



Am Sonntag, 21. Mai 2017 16:49:39 UTC+2 schrieb Anton Bacaj:
I might be wrong but, once you do attachToTarget, try using `Page` (it will have the new target).
On Sun, May 21, 2017 at 10:32 AM, <xanato...@gmail.com> wrote:
CDP(chromeOptions).then((client) => {
// extract domains
const {Network, Page, Target} = client;

Target.setDiscoverTargets({discover: true});

Target.targetCreated((params) => {
console.log("Target Created");
if(params.targetInfo.type != "other")
return;
console.log("Target Attaching");
Target.attachToTarget({targetId: params.targetInfo.targetId}).then((test)=>{
debugger;
})
});

Target.targetDestroyed((params) => {
console.log("Target Destroyed");
});



now it says its attached, but i don't see any resources of the new windows being loaded in
Network.responseReceived

do I need a new instance of the debugger to work?

--
You received this message because you are subscribed to the Google Groups "headless-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to headless-dev...@chromium.org.

To post to this group, send email to headle...@chromium.org.

Eric Seckler

unread,
May 22, 2017, 3:32:26 AM5/22/17
to xanato...@gmail.com, headless-dev
If you use Target.attachToTarget, your interaction with the new target needs to go through Target.sendMessageToTarget and Target.receivedMessageFromTarget. An alternative is to open a new devtools socket to the new target (using the target parameter in the CDP constructor options), then you can use the domains directly as usual.

Reply all
Reply to author
Forward
0 new messages