M H R
M H R

Reputation: 11

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

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?

Upvotes: 0

Views: 531

Answers (2)

M H R
M H R

Reputation: 11

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:

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193298

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

Upvotes: 0

Related Questions