These are chat archives for rosshinkley/nightmare

29th
Nov 2016
zypjerry
@zypjerry
Nov 29 2016 06:48
Hi,everyone .I have a problem about Click the button using Electron and Nightmare.Because it handle POST navigation for new windows.so is special.

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });

nightmare
.goto('http://football-system.jp/fss/pub_taikaigamelist.php?lid=eBVesRz5C54=')
.wait(1000)
.click('a[href="javascript:void(0);"]')
.wait(1000)

    .then()
  .catch(function (error) {
    console.error('Search failed:', error);
  });
sorry first use it
``` 
```

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });

nightmare
.goto('http://football-system.jp/fss/pub_taikaigamelist.php?lid=eBVesRz5C54=')
.wait(1000)
.click('a[href="javascript:void(0);"]')
.wait(1000)

    .then()
  .catch(function (error) {
    console.error('Search failed:', error);
  });
That is my code. I want to click the button and get the HTML of the next page. But I want to use Nightmare and Electron. I don't know how to do this. Maybe about the async and form submit and action.
Ændrew Rininsland
@aendrew
Nov 29 2016 14:34
@Mr0grog Cheers, thanks for that — but IIRC, await acts almost like the value of .then() being returned? So what do I do if I need to, say, evaluate something on page, await the result of that, navigate somewhere else, then run .end()?
I have a script I’m playing with that is a good example — https://github.com/aendrew/scrape-routerpasswords.com/blob/master/index.js
I end up just calling process.exit()at the end of the script, but that feels hack-y. If I don’t, and have {show: true} when constructing Nightmare, the Nightmare process doesn’t seem to end after the loop.
Is the answer just to always construct a new Nightmare instance between workflow steps where you need to return an .evaluate()?
Rob Brackett
@Mr0grog
Nov 29 2016 17:52
what do I do if I need to, say, evaluate something on page, await the result of that, navigate somewhere else, then run .end()?
async function doStuff () {
  const nightmare = Nightmare();
  const evaluationResult = await nightmare
    .goto('http://somewhere.com')
    .evaluate(() => {
      // do something and return a useful value
    });
  const secondEvaluationResult = await nightmare
    .goto('http://somewhereelse.com')
    .evaluate(() => {
      // do stuff
    });
  await nightmare.end();
  // and finally return your results
  return {
    first: evaluationResult,
    second: secondEvaluationResult
  };
}
You should definitely avoid needing to call process.exit

The key here is that await takes any thennable and a nightmare instance is a thennable.

You always need to call .end() to "close" the browser, but it certainly doesn't need to be part of an existing chain of actions. You can wait until everything is done and then await .end() as in the example above.

The one thing you do need to make sure you do is set up multiple nightmare queues and run them in parallel. As long as you are always using await, you don't have to worry about that, though.

Rob Brackett
@Mr0grog
Nov 29 2016 17:57
@aendrew in your linked example, the problem is that you aren't awaiting nightmare.end(). It's a super confusing part of nightmare's API, but .end() doesn't do anything on its own. All it does it add a little flag that tells nightmare to close the browser when it is done with the current set of actions. So until you await nightmare or call then() on it, no actions execute and nightmare doesn't actually close.
Ændrew Rininsland
@aendrew
Nov 29 2016 17:58
@Mr0grog Ah! Okay! That makes sense! Awesome, many thanks! :D
Rob Brackett
@Mr0grog
Nov 29 2016 17:59
np
there are more than a few weird thorny corners in Nightmare's API, unfortunately.
Ændrew Rininsland
@aendrew
Nov 29 2016 18:00
Eh, beats the heck out of using Beautiful Soup for page scraping! :D
Rob Brackett
@Mr0grog
Nov 29 2016 18:00
heh, I won't necessarily disagree with that at all :P
Ændrew Rininsland
@aendrew
Nov 29 2016 18:01
I’m actually working on a tutorial using it for this month’s JournoCoders London, it’s totally a tool that more data journalists need to know about. Will make sure to drop a link in here once I’ve published!
*”it” being Nightmare, rather; not BeautifulSoup.
Rob Brackett
@Mr0grog
Nov 29 2016 18:01
Have you seen https://github.com/rosshinkley/nightmare-examples ? It has some good tips.
kenneth koontz
@kennethkoontz
Nov 29 2016 20:31
I'm using nightmare to print a report. In production, I have ubuntu server with xvfb running. My problem is that when I print the report, it doesn't appear to use print style media queries. Any suggestions? It works fine when I run nightmare locally.
Rob Brackett
@Mr0grog
Nov 29 2016 21:39
This probably doesn't help a whole lot, but this sounds like it might be an issue in Electron. Have you looked through the issues there?
(Also, is your local environment, where it is working, Ubuntu as well? Or is it an entirely different platform, not just the same platform with a GUI running?)