Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium Headless Browser Mode

Selenium Headless Browser Mode

Headless Mode refers to running the browser without a graphical user interface (GUI); in other words, the browser runs in the background and does not pop up a visible window.

Headless mode is typically used in scenarios such as automated testing, web scraping, and performance testing, because it saves system resources and can run normally on servers without a display.

Why use headless browser mode?


Headless Browser Mode in Selenium

Set Chrome Headless Mode

In Selenium, you can use ChromeOptions class to set Chrome's headless mode. Here is a simple example:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

from

selenium.

webdriver

.

common

.

by

import

By

from

selenium.

webdriver

.

chrome

.

options

import

Options

Set Chrome Headless Mode

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--headless"

)

 

Enable headless mode

chrome_options.

add_argument

(

"--disable-gpu"

)

 

Disable GPU Acceleration

Create a WebDriver Instance

driver

=

webdriver.

Chrome

(

service

=

Service

(

'/path/to/chromedriver'

)

,

options

=

chrome_options

)

Open a web page

driver.

get

(

"https://www.baidu.com"

)

Print the Web Page Title

print

(

driver.

title

)

Close the browser

driver.

quit

(

)

Set Firefox Headless Mode

For the Firefox browser, you can use FirefoxOptions class to set headless mode. Here is an example:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

firefox

.

service

import

Service

from

selenium.

webdriver

.

firefox

.

options

import

Options

Set Firefox Headless Mode

firefox_options

=

Options

(

)

firefox_options.

add_argument

(

"--headless"

)

 

Enable headless mode

Create a WebDriver Instance

driver

=

webdriver.

Firefox

(

service

=

Service

(

'/path/to/geckodriver'

)

,

options

=

firefox_options

)

Open a web page

driver.

get

(

"https://www.example.com"

)

Print the Web Page Title

print

(

driver.

title

)

Close the browser

driver.

quit

(

)

Set Edge Headless Mode

For the Edge browser, you can use EdgeOptions class to set headless mode. Here is an example:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

edge

.

service

import

Service

from

selenium.

webdriver

.

edge

.

options

import

Options

Set Edge Headless Mode

edge_options

=

Options

(

)

edge_options.

add_argument

(

"--headless"

)

 

Enable headless mode

Create a WebDriver Instance

driver

=

webdriver.

Edge

(

service

=

Service

(

'/path/to/msedgedriver'

)

,

options

=

edge_options

)

Open a web page

driver.

get

(

"https://www.example.com"

)

Print the Web Page Title

print

(

driver.

title

)

Close the browser

driver.

quit

(

)


Common Issues and Solutions in Headless Mode

Page Does Not Load Completely

In headless mode, sometimes the page may not load completely, causing elements to be unfindable. This can be resolved as follows:

Screenshots and Logs

In headless mode, you may need to take screenshots or record logs to debug issues. You can use the following methods:

Headless browser mode is a powerful tool in Selenium automated testing and web scraping. With simple settings, you can run the browser without a graphical interface, saving resources and improving efficiency. Whether it is Chrome, Firefox, or Edge, Selenium 4 provides simple methods to enable headless mode.

Here is a complete code example of using Selenium in headless mode to capture the Baidu homepage (www.baidu.com).

The code enables Chrome's headless mode and captures a screenshot of the page saved as a file.

Example

import

time

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

chrome

.

options

import

Options

from

selenium.

webdriver

.

common

.

by

import

By

Configure Chrome Options

chrome_options

=

Options

(

)

chrome_options.

add_argument

(

"--headless"

)

 

Enable headless mode

chrome_options.

add_argument

(

"--disable-gpu"

)

 

Disable GPU Acceleration

chrome_options.

add_argument

(

"--window-size=1920,1080"

)

 

Set window size

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

chrome_options

)

try

:

   

Open the Baidu home page

    driver.

get

(

"https://www.baidu.com"

)

   

Wait for the Page to Finish Loading

   

time

.

sleep

(

2

)

 

You can adjust the wait time as needed

   

Force Stop Page Loading

    driver.

execute_script

(

"window.stop();"

)

   

Print the page title

   

print

(

"Page title:"

,

driver.

title

)

   

Capture and Save the Page Screenshot

    screenshot_path

=

"baidu_screenshot.png"

    driver.

save_screenshot

(

screenshot_path

)

   

print

(

f

"Screenshot saved to: {screenshot_path}"

)

finally

:

   

Close the browser

    driver.

quit

(

)

After successful execution, it will output:

页面标题: 百度一下,你就知道

截图已保存到: baidu_screenshot.png

Opening the current directory, you will see the baidu_screenshot.png file, which looks like this when opened: