How to hide Firefox window (Selenium WebDriver)?


We can hide the Firefox window in Selenium webdriver. This can be done by making the browser headless. We shall achieve this with the FirefoxOptions class. We shall then create an object option of that class.

We have to make the browser setting options.headless to True value. This driver object shall then receive this information. We need to have the import statement: from selenium.webdriver.firefox.options import Options as FirefoxOptions for adding the FirefoxOptions class.

Syntax

options = webdriver.FirefoxOptions()
options.headless = True

Example

Code Implementation.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
#object of FirefoxOptions
options = webdriver.FirefoxOptions()
#setting headless parameter
options.headless = True
driver = webdriver.Firefox(executable_path="C:\geckodriver.exe", options=options)
driver.implicitly_wait(0.8)
driver.get("https://www.tutorialspoint.com/tutorialslibrary.htm")
#identify element
n = driver.find_element_by_xpath("//*[text()='Library']")
#perform click
n.click();
print("Page title after click: " + driver.title)
driver.quit()

Output

Updated on: 28-Dec-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements