Nightmare.js is a library for automating browsers in a Node.js environment. You can automate various operations such as taking screenshots, submitting forms, and page navigation. This article explains the installation method and basic usage of Nightmare.js in detail.
Installing Nightmare.js
First, let’s install Nightmare.js in your project. After confirming that Node.js and npm are installed, run the following command.
npm install nightmare
This will install Nightmare.js in your project.
Debugging
Debug as follows.
set DEBUG=nightmare & node C:\document\js\nightmare-test.js
DEBUG=nightmare
Basic Usage
Nightmare.js Initial Setup
To use Nightmare.js, you first need to create an instance. The following code shows the basic initial setup for Nightmare.js.
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
By setting the { show: true } option, the browser window will be displayed. This is useful for debugging and testing.
Page Navigation
Let’s see how to navigate to a web page using Nightmare.js.
nightmare
.goto('https://example.com')
.then(() => console.log('Page loaded'))
.catch(error => console.error('Failed to load the page', error));
Use the goto method to navigate to the specified URL. Process what happens after the page loads with the then method, and handle errors with the catch method.
Form Input and Submission
Let’s see how to use Nightmare.js to fill in and submit forms.
nightmare
.goto('https://example.com/login')
.type('#username', 'yourUsername')
.type('#password', 'yourPassword')
.click('#loginButton')
.wait('#dashboard')
.then(() => console.log('Login successful'))
.catch(error => console.error('Login failed', error));
Use the type method to input text into form input fields, and the click method to click buttons. Use the wait method to wait for the next page to load.
Taking Screenshots
Let’s see how to take screenshots of web pages using Nightmare.js.
nightmare
.goto('https://example.com')
.screenshot('example.png')
.then(() => console.log('Screenshot saved'))
.catch(error => console.error('Failed to take screenshot', error));
Use the screenshot method to save a screenshot with the specified filename.
Getting Page Content
Let’s see how to get the content of a web page using Nightmare.js.
nightmare
.goto('https://example.com')
.evaluate(() => document.querySelector('h1').innerText)
.then(text => console.log('Page title:', text))
.catch(error => console.error('Failed to get page content', error));
Use the evaluate method to execute JavaScript in the browser and get the results.
DuckDuckGo Sample
The following code performs a search on DuckDuckGo and gets the first link from the search results.
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
nightmare
.goto('https://duckduckgo.com')
.type('#search_form_input_homepage', 'github nightmare')
.click('#search_button_homepage')
.wait('#zero_click_wrapper .c-info__title a')
.evaluate(function () {
return document.querySelector('#zero_click_wrapper .c-info__title a').href;
})
.end()
.then(function (result) {
console.log(result);
})
.catch(function (error) {
console.error('Search failed:', error);
});
Summary
Nightmare.js is a powerful tool for browser automation in a Node.js environment. From basic installation methods to page navigation, form input, screenshot capture, and page content retrieval, you can automate various operations. Use this to achieve efficient web scraping and automation tasks.