segment-boneyard / nightmare

A high-level browser automation library.
https://open.segment.com
19.54k stars 1.08k forks source link

How to interact with a new generated window? #148

Closed giggioz closed 9 years ago

giggioz commented 9 years ago

Hi,

i have a Nightmare instance in which i click a button that opens a new window.

Is there a way to handle the DOM of the new window? (for example typing the inputs, clicking buttons...)

Thanks.

simonholmes commented 9 years ago

I have the same requirement.

I'd need to be able to click on a link that calls a JavaScript function; amongst other things this function generates a URL and pops it up in a new window.

I see PhantomJS has the ability to do this, added in this PR

giggioz commented 9 years ago

Due to the lack of answers i moved to casperjs, it solved all the problems i had and it works nicely. You can write clean code avoiding callback hell.

Concerning this single issue in casperjs you can interact with a pop up window with e specific method, look here http://docs.casperjs.org/en/latest/modules/casper.html#waitforpopup

Hope this helps.

simonholmes commented 9 years ago

Thank you! I'll try Casper out too :)

bchr02 commented 9 years ago

Still no answer. I guess I'll have to try Casper too.

reinpk commented 9 years ago

sorry fellas, been slammed but will get to this on the coming weekend i hope

bchr02 commented 9 years ago

okay, thank you.

Based on your response I can only assume that it's currently not a feature of the api?

spencerwi commented 9 years ago

Any word on this feature? Due to the pages I have to scrape, it's a make-or-break for me.

bchr02 commented 9 years ago

@reinpk Can the feature to interact with new window be added to v2?

reinpk commented 9 years ago

not sure yet, great question though. we'll need to dig into the electron documentation to figure it out

reinpk commented 9 years ago

i don't believe this is possible in electron, so i'm going to close this as not possible unfortunately. if you have an example of a popup script would be great to test it out though

bchr02 commented 9 years ago

@reinpk that's too bad. I was hoping for this feature to come now with v2 out.

I wish I understood more on how you are using electron in place of phantomjs. Maybe then I would understand why you say that you don't think it's possible.

Any chance you can explain at a high level how you are using electron? It's my understanding that electron is used to package applications...

bchr02 commented 9 years ago

Nevermind I think I found the answer in your readme. It appears you are using electron to create a new browser window and subsequently navigate the web. Very cool.

Maybe there is a way to force input buttons that a given website tries to open a new window so that they open in the current page. Anybody have any ideas?

bchr02 commented 9 years ago

@reinpk BrowserWindow.getAllWindows() returns an array of all the BrowserWindow object references. Surely this could be used to incorporate some sort of interaction with newly generated windows.

cc @matthewmueller

devilpreet commented 7 years ago

I tried what @bchr02 said "BrowserWindow.getAllWindows()" and this workaround works.

machadogj commented 7 years ago

@bchr02 @devilpreet could you please paste a code sample of how you guys made this work? Thanks!

devilpreet commented 7 years ago

I wanted to get the newly opened window URL and heres how I got it working

const BrowserWindow = require('electron').BrowserWindow;
      var wins = BrowserWindow.getAllWindows();
      console.log("BW All Length: " + wins.length);

      //Last one will be the newly opened window
      var url = wins[wins.length - 1].webContents.getURL();
      console.log("URL: " + url);

      //The parent window
      var url2 = wins[0].webContents.getURL();
      console.log("URL: " + url2);