How to detect if Chrome browser is headless in selenium?
Asked Answered
P

5

15

I'm writing a selenium test which has different behavior given whether the chrome browser was started as headless or not. My question is in my test how do I detect if the browser is headless for my conditional flow?

Paronym answered 13/7, 2018 at 14:21 Comment(1)
I don't understand why this question has negative votes. It is a valid question!Passim
M
7

https://antoinevastel.com/bot%20detection/2018/01/17/detect-chrome-headless-v2.html#:~:text=In%20order%20to%20automate%20Chrome,possible%20to%20detect%20Chrome%20headless.

driver.execute_script("return navigator.plugins.length == 0")
Midbrain answered 11/9, 2020 at 10:50 Comment(1)
The linked post was updated, smees that now driver.execute_script("return navigator.webdriver") is True is the wayLoyola
K
3

Considering this scenario:

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

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920,1080")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

In my testing I found that driver.get_window_position() returns {'x': 0, 'y': 0} when running in headless mode. However when run without the headless option it defaults to {'x': 10, 'y': 10}.

I'm using this method to determine if my Chrome instance is running in headless mode by simply doing:

assert driver.get_window_position()['x'] == 0
assert driver.get_window_position()['y'] == 0
Kling answered 29/12, 2022 at 16:39 Comment(0)
S
1

I just find this way for that.

Options its a list, so you have to find the "--headless" element in there.

opc = Options()
opc.add_argument('headless')

in this case, the position of the element in the list is [0], so you only have to to something like this:

if (opc.arguments[0]=="--headless"):
    print("Do something")
Simpson answered 20/4, 2022 at 20:38 Comment(0)
S
0

I know the question is about Chrome, but if you're using Firefox, there is a cleaner solution (tested on Firefox 106):

if driver.caps.get("moz:headless", False):
     print("Firefox is headless")
Sonny answered 3/11, 2022 at 10:9 Comment(0)
S
-1

You need to explicitly add the argument "--headless" to your chromeOptions object when starting an instance of chrome headlessly. If you're writing, for example, a test framework for a website you probably have some sort of browser creator class that is able to give you different browsers to work with. Why not save that argument as an additional member of that class?

Another simpler option if you don't have a that sort of factory design in your code is just

options = webdriver.ChromeOptions
options.add_argument("--headless")
print(options.arguments)
Say answered 13/7, 2018 at 14:43 Comment(5)
Thank you but I am not really looking for validation that the browser started headless or not. I want to check this in the conditional flow of my test. Such as: if headless: do this else: do thisParonym
if "--headless" in options.arguments: print("headless as fuh") print("doing condintionally headless test things now")Say
My bad I should make the situation a bit clearer. The options setup is in a general class and so options is not available in the test itself. The only thing that is available in the test is the initialized driver. So given that all I have access to is the driver is there any property of the driver i.e. webdriver.Chrome that I can use to detect the headless attribute.Paronym
Have you had any luck with this? I've been trying to comb through the documentation for selenium and I'm having a hard time finding out whether or not there is a way to see if the driver is headless from only accessing the driver itself.Say
Unfortunately not. I've looked through all of the initialized drivers attributes to find any indication of headless but haven't found anything.Paronym

© 2022 - 2024 — McMap. All rights reserved.