How to hide Firefox window (Selenium WebDriver)?

SeleniumAutomation TestingTesting Tools

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

raja
Published on 28-Dec-2020 15:32:43
Advertisements