puppet
puppet

Reputation: 727

How to detect if selenium initializer driver is headless

Let's say I have this code

options = webdriver.ChromeOptions()

options.add_argument('--headless')
options.add_argument("window-size=1920,1080")

browser=webdriver.Chrome(options=options,executable_path=r"chromedriver.exe")
browser.execute_cdp_cmd('Network.setUserAgentOverride', 
                                {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})

How can I check if the initialized browser is headless or not, programmatically? I mean, if I type browser.get_window_size() I get {'width': 1920, 'height': 1080}, if I write browser.execute_script('return navigator.languages') it returns ['en-US', 'en']

What I'm looking for is something like browser.is_headless() where I can get if a given browser is headless or not.

Upvotes: 3

Views: 954

Answers (3)

scūriolus
scūriolus

Reputation: 968

If you're using Firefox (tested on Firefox 106):

if driver.caps.get("moz:headless", False):
     print("Firefox is headless")

Upvotes: 0

Tomi Monaco
Tomi Monaco

Reputation: 69

options = webdriver.ChromeOptions()
options.headless

Will return True, if --headless argument is set into ChomeOptions(), otherwise, will return False.

Upvotes: 1

kinshukdua
kinshukdua

Reputation: 1994

Based on the official Selenium documentation

options.headless

should return whether headless is set or not

Upvotes: -1

Related Questions