add_argument not working to enable dark mode in Google Chrome with Selenium WebDriver

236 Views Asked by At

I'm trying to launch Google Chrome with dark mode enabled using Selenium WebDriver in Python, but I'm having trouble getting the add_argument method to work. Here's the code I'm using:

from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
import time

chrome_options = Options()
chrome_options.add_argument('--force-dark-mode')

driver = Chrome(options=chrome_options)
time.sleep(5)
driver.quit()

When I run this code, it launches Google Chrome, but dark mode is not enabled. I've also tried using the --force-dark-mode-on but they didn't work either.

I'm using Google Chrome version 78, which should support the --force-dark-mode option. I'm not seeing any error messages or warnings in the console or logs.

What could be causing add_argument not to work for enabling dark mode in Google Chrome with Selenium WebDriver? Are there any other options or workarounds that I can try?

2

There are 2 best solutions below

1
undetected Selenium On

Chrome in dark mode

Not sure about Google Chrome version 78 but using Selenium v4.10.0 with / ChromeDriver v114.0 can be enforced using the argument:

options.add_argument('--force-dark-mode')
  • Code block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument('--force-dark-mode')
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options)
    driver.get("https://www.google.com/")
    
  • Browser snapshot:

dark_mode


Web components in dark mode

Auto for web contents can be enforced by using the argument:

options.add_argument("--enable-features=WebContentsForceDark")
  • Code block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("--enable-features=WebContentsForceDark")
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(options=options)
    driver.get("https://www.google.com/")
    
  • Browser snapshot:

enable-features=WebContentsForceDark

1
M H R On

fix above code :

chrome_options.add_argument("--enable-features=WebContentsForceDark")

and this is my command in chrome://version

Command Line:

/usr/bin/google-chrome-stable --flag-switches-begin --enable-features=WebContentsForceDark --flag-switches-end --desktop-startup-id=gnome-shell/Google Chrome/some-info

For more information about command-line options in Google Chrome, you can check out the official documentation: