What is Headless Browser
A web browser without a graphical user interface, controlled programmatically. Used for automation, testing, and other purposes.
A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell. For example, you may want to run some tests against a real web page, create a PDF of it, or just inspect how the browser renders an URL.
Starting Headless Chrome
The easiest way to get started with headless mode is to open the Chrome binary from the command line. If you've got Chrome 59+ installed, start Chrome with the --headless flag:
chrome \
--headless \ # Runs Chrome in headless mode.
--disable-gpu \ # Temporarily needed if running on Windows.
--remote-debugging-port=9222 \
https://www.chromestatus.com # URL to open. Defaults to about:blank.
Chrome should point to your installation of Chrome. The exact location will vary from platform to platform. For window it should C:\Program Files (x86)\Google\Chrome\Application so change directory accordingly.
Once we are inside the directory which has Chrome application we run below command :
chrome --headless --disable-gpu --remote-debugging-port=9222 https://www.ietf.org/
This command will open URL in headless Chrome but since we don't have browser UI to see the page, We need to navigate to http://localhost:9222
in another browser(non-headless) to check that everything is working. You'll see a list of the headless Chrome instances running. You can click through and see what Headless is rendering:
When you run Chrome with --remote-debugging-port=9222
, it starts an instance with the DevTools protocol enabled. The protocol is used to communicate with Chrome and drive the headless browser instance.
Comments
0 comments
Please sign in to leave a comment.