DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new') on Selenium 4.8.0 Python
Asked Answered
M

2

14

I am trying to execute a basic program using Selenium 4.8.0 Python clients in headless mode:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
options.headless = True
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')
driver.quit()

With the following configuration:

  • Selenium 4.8.0 Python
  • Chrome _Version 109.0.5414.120 (Official Build) (64-bit)
  • ChromeDriver 109.0.5414.25

Though the program gets executed successfully there seems to a DeprecationWarning as:

DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new')

Can anyone explain the DeprecationWarning and the required changes?

Mulder answered 6/2, 2023 at 22:6 Comment(1)
Relevant selenium blog post: selenium.dev/blog/2023/headless-is-going-awayClio
C
22

The deprecation of the headless property was announced in the Selenium Blog post Headless is Going Away! (archive) on January 29, 2023. The summary and suggested changes are as follows:

Headless is Going Away!

Headless is an execution mode for Firefox and Chromium based browsers. It allows users to run automated scripts in headless mode, meaning that the browser window wouldn’t be visible. In most of Selenium’s bindings there is a convenience method to set this execution mode while setting the browser options. However, Selenium 4.8.0 will be deprecated [sic] this method and now users need to set it through arguments when setting the browser options.

[...]

How can I set headless mode from now on?

In short, users can add the headless mode they want to use through arguments in browser options.

Before

options = ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('http://selenium.dev')
driver.quit()

After

options = ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
driver.get('http://selenium.dev')
driver.quit()

See the full blog post for additional background on why this change was implemented.

Clio answered 6/2, 2023 at 22:19 Comment(0)
M
9

Test Automation developers had been using Headless Chrome and Firefox Headless for quite sometime now to execute the automated scripts in headless mode where the browser window wouldn't be visible. This was the traditional headless mode which now turns the old Headless mode.

The snippets being used were:

  • Java:

    ChromeOptions options = new ChromeOptions();
    options.setHeadless(true);
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://selenium.dev");
    driver.quit();
    
  • Python:

    options = ChromeOptions()
    options.headless = True
    driver = webdriver.Chrome(options=options)
    driver.get('http://selenium.dev')
    driver.quit()
    
  • Javascript:

    let driver = await env
      .builder()
      .setChromeOptions(new chrome.Options().headless())
      .build();
    await driver.get('https://selenium.dev');
    await driver.quit();
    

According to this Selenium Blog this old headless mode will be still available by using the --headless switch with no value or with old value. This convenient yet deprecated method will be removed in Selenium 4.10.0


Renaming NativeHeadlessChrome to new Headless

Recently Chromium team have released the Native Headless mode which is now officially called the new Headless mode. This functionality have landed with:

aptly supported through:

The new syntax requires --headless=new to be passed as an argument, where as we passed only --headless while using Chrome since v96 till v108.

Sample Code snippets:

  • Java:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless=new");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://selenium.dev);
    driver.quit();
    
  • Python:

    options = ChromeOptions()
    options.add_argument("--headless=new")
    driver = webdriver.Chrome(options=options)
    driver.get('http://selenium.dev')
    driver.quit()
    
  • Javascript:

    let driver = await env
      .builder()
      .setChromeOptions(options.addArguments('--headless=new'))
      .build();
    await driver.get('https://selenium.dev');
    await driver.quit();
    
  • CSharp:

    var options = new ChromeOptions();
    options.AddArgument("--headless=new");
    var driver = new ChromeDriver(options);
    driver.Navigate().GoToUrl("https://selenium.dev");
    driver.Quit();
    
  • Ruby:

    options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
    driver = Selenium::WebDriver.for :chrome, options: options
    driver.get('https://selenium.dev')
    driver.quit
    
Mulder answered 10/2, 2023 at 23:47 Comment(1)
no wonder is this applicable to portable chrome v109 ?Discotheque

© 2022 - 2024 — McMap. All rights reserved.