Nightmare is a high-level browser automation library for Node.js, which is based on Electron. While Nightmare itself is not specifically designed for DOM manipulation, it provides methods to interact with web pages in a way that can indirectly manipulate the DOM by executing JavaScript code within the context of the webpage.
Here are some of the methods available in Nightmare for interacting with web pages and indirectly manipulating the DOM:
.goto(url)
: This method navigates to the specified URL.nightmare.goto('http://example.com');
.type(selector, text)
: This method types the provided text into the input element matched by the selector.nightmare.type('#search', 'nightmare');
.click(selector)
: This method performs a click on the element matched by the selector.nightmare.click('#submit-button');
.evaluate(fn, [arg1, arg2,...])
: This method evaluates the provided function on the page with optional arguments. This is where you can perform more complex DOM manipulations by passing in a function that interacts with the page's JavaScript.nightmare.evaluate(() => { document.querySelector('#element').innerHTML = 'New Content'; });
.insert(selector, text)
: This method clears the value of the input element matched by the selector and then types the provided text into it.nightmare.insert('#textfield', 'new text');
.check(selector)
: This method checks a checkbox matched by the selector.nightmare.check('#agree');
.uncheck(selector)
: This method unchecks a checkbox matched by the selector.nightmare.uncheck('#agree');
.select(selector, optionValue)
: This method selects an<option>
element within a<select>
element by its value.nightmare.select('#dropdown', 'optionValue');
.scrollTo(top, left)
: This method scrolls the page to the specified position.nightmare.scrollTo(500, 0);
.viewport(width, height)
: This method sets the viewport size.nightmare.viewport(1280, 800);
.screenshot([path], [clip])
: This method takes a screenshot of the current page, with optional arguments for saving to a path and clipping to a rectangle.nightmare.screenshot('screenshot.png');
.html(path, [saveType])
: This method saves the current page's HTML to the specified path.nightmare.html('page.html', 'HTMLComplete');
.end()
: This method ends the Nightmare instance and closes Electron.nightmare.end();
To manipulate the DOM more directly, you would typically use .evaluate()
to run any JavaScript code you want. Here's an example of how you might use .evaluate()
to hide an element:
nightmare
.goto('http://example.com')
.evaluate(() => {
document.querySelector('.some-element').style.display = 'none';
})
.then(() => {
// Continue with other actions after the DOM manipulation
});
Remember that Nightmare runs in a Node.js environment, so it's asynchronous and relies on promises or async/await for control flow. Also, note that since Nightmare is based on Electron, it is not as lightweight as some other headless browsers like Puppeteer (which is based on Chromium) and may not be suitable for all use cases, especially those requiring a large number of concurrent browser instances.