Reputation: 11
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
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
Reputation: 193298
Not sure about Google Chrome version 78 but using Selenium v4.10.0 with google-chrome / ChromeDriver v114.0 darkmode 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:
Auto darkmode 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:
Upvotes: 0