Selenium WebDriver has a feature called Headless mode, which allows you to run your tests or web scrapers without a GUI or in the background. It's useful when you want to run your scripts on a virtual server or need to speed up the execution.
Here is how you can run your Selenium scraper headless in Python and JavaScript:
Python
In Python, you can use webdriver.Chrome
or webdriver.Firefox
with specific options to enable headless mode.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=options, executable_path='path_to_your_chromedriver')
driver.get("http://www.google.com/")
print(driver.title)
driver.quit()
Replace 'path_to_your_chromedriver'
with the actual path to your chromedriver
file.
JavaScript
For JavaScript (Node.js), you can use selenium-webdriver
package. Here is how you can enable headless mode:
const {Builder, By, Key, until} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
let options = new chrome.Options();
options.addArguments("--headless");
let driver = new Builder().forBrowser('chrome').setChromeOptions(options).build();
driver.get('http://www.google.com/');
driver.getTitle().then(title => {
console.log(title);
});
driver.quit();
In both examples, the script navigates to http://www.google.com and prints out the title of the page.
Please note that to use Chrome in headless mode, you need to have Chrome version 59 or up for Linux and macOS, or version 60 or up for Windows. Also, you need to have the appropriate version of chromedriver
or geckodriver
(for Firefox) installed and available in your system's PATH.