I am trying to run my code in headless mode (it runs fine without headless). I have tried almost every option given on the internet.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.service import Service
from fake_headers import Headers
options = Options()
options.binary_location = '/snap/brave/current/opt/brave.com/brave/brave'
options.add_argument("--user-data-dir=/home/dhruv/snap/brave/current/.config/BraveSoftware/Brave-Browser/")
options.add_argument("--profile-directory=Default")
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-web-security")
options.add_argument("--disable-xss-auditor")
options.add_argument("--allow-running-insecure-content")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_argument("--remote-debugging-port=9222")
options.add_argument("--window-size=1366,768")
options.add_argument("-disable-gpu")
options.add_experimental_option("detach", True)
user_agent = Headers(
browser="chrome", os="lin", headers=False
).generate()['User-Agent']
# user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.180 Safari/537.36'
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
# user_agent = 'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
options.add_argument(f'user-agent="{user_agent}"')
driver_path = '/home/dhruv/Downloads/chromedriver-linux64/chromedriver'
service = Service(executable_path=driver_path, log_path = '/home/dhruv/python/chromedriver.log')
driver = webdriver.Chrome(options = options, service = service)
try :
driver.get("brave://settings/appearance")
driver.get_screenshot_as_file("screenshot.png")
elm = driver.find_element(By.CSS_SELECTOR, "settings-ui").shadow_root\
.find_element(By.CSS_SELECTOR, "settings-main").shadow_root\
.find_element(By.CSS_SELECTOR, "settings-basic-page").shadow_root\
.find_element(By.CSS_SELECTOR, "[section=appearance] settings-appearance-page").shadow_root\
.find_element(By.CSS_SELECTOR, "settings-animated-pages settings-brave-appearance-theme").shadow_root\
.find_element(By.CSS_SELECTOR, "#braveThemeType")
select = Select(elm)
if select.first_selected_option.get_attribute("value") == "1":
select.select_by_value("2")
else:
select.select_by_value("1")
finally :
driver.close()
driver.quit()
I still get the error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"settings-ui"}
(Session info: headless chrome=116.1.57.62); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
In the second line of the error it says session is headless chrome=116.1.57.62
. What should I do now? Totally exhausted on the options.
I am using Brave browser, selenium version 4.11.2, chromium version 116.0.5845.180
According to latest update the new way of using headless worked for me. Specify --headless=new
instead of --headless
.