How to hide Firefox window (Selenium WebDriver)?

PythonSeleniumFirefoxWebdriver

Python Problem Overview


When I execute multiple test simultaneously, i don't want to keep Firefox browser window visible.. I can minimize it using selenium.minimizeWindow() but I don't want to do it.

Is there any way to hide Firefox window? I am using FireFox WebDriver.

Python Solutions


Solution 1 - Python

Python

The easiest way to hide the browser is to install PhantomJS. Then, change this line:

driver = webdriver.Firefox()

to:

driver = webdriver.PhantomJS()

The rest of your code won't need to be changed and no browser will open. For debugging purposes, use driver.save_screenshot('screen.png') at different steps of your code or just switch to the Firefox webdriver again.

On Windows, you will have to specify the path to phantomjs.exe:

driver = webdriver.PhantomJS('C:\phantomjs-1.9.7-windows\phantomjs.exe')

Java

Have a look at Ghost Driver: https://stackoverflow.com/questions/14122185/how-to-run-ghostdriver-from-java


C#

https://stackoverflow.com/questions/45233065/how-to-hide-firefoxdriver-using-selenium-or-put-it-in-cform

Solution 2 - Python

Just add the following code.

import os
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()

Solution 3 - Python

Finally I found the solution for those who are using windows Machine for running the Tests using any method. Well, implementation is not in Java, but you can do it very easily.

Use AutoIt tool. It has all the capability to handle windows. It is a free tool.

  1. Install AutoIt: http://www.autoitscript.com/site/autoit/downloads/

  2. Open the Editor and write below code for Hiding any window.

    AutoItSetOption("WinTitleMatchMode", 2)
    WinSetState("Title Of Your Window", "", @SW_HIDE) 
    
  3. To Unhide it, you can use below line of code.

    AutoItSetOption("WinTitleMatchMode", 2)
    WinSetState("Title Of Your Window", "", @SW_SHOW)
    

    WinTitleMatchMode has different options which can be used to match Windows title.

    1 = Match the title from the start (default)`
    2 = Match any substring in the title
    3 = Exact title match
    4 = Advanced mode, see Window Titles & Text (Advanced)
    

So, what I've done is: I have created an .exe file of a small program and passed a parameter as a command line argument as below.

Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");

in HideNSeek.exe - I have below AutoIt Code:

AutoItSetOption("WinTitleMatchMode", 1) 

if $CmdLine[0] > 0 Then
	if $CmdLine[1] == 0 Then
		WinSetState($CmdLine[2], "", @SW_HIDE)    
	ElseIf $CmdLine[1] == 1 Then
		WinSetState($CmdLine[2], "", @SW_SHOW)    		
	Else	
	EndIf	
EndIf

$CmdLine[] is an array, which will have all command line parameters...

$CmdLine[0] = number of Parameter
$CmdLine[1] = 1st Parameter after Exe Name 
...

If there is any space in the Window Title, then you have to use double quotes to pass it as a command line parameter like above.

Below Line of code will execute AutoIt exe and if I pass '0' in 1st parameter then it will hide the window and if I will pass '1' then it will unhide windows matching the title.

Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");

I hope this will help you. Thanks!

Solution 4 - Python

Just do (Python):

opts = webdriver.FirefoxOptions()
opts.headless = True
firefox = webdriver.Firefox(options=opts)

Solution 5 - Python

I used xvfb to solve the problem like this.

First, install Xvfb:

# apt-get install xvfb

on Debian/Ubuntu; or

# yum install xorg-x11-Xvfb

on Fedora/RedHat. Then, choose a display number that is unlikely to ever clash (even if you add a real display later) – something high like 99 should do. Run Xvfb on this display, with access control off:

# Xvfb :99 -ac

Now you need to ensure that your display is set to 99 before running the Selenium server (which itself launches the browser). The easiest way to do this is to export DISPLAY=:99 into the environment for Selenium. First, make sure things are working from the command line like so:

$ export DISPLAY=:99
$ firefox

or just

$ DISPLAY=:99 firefox

Below there is a link that helped me
http://www.alittlemadness.com/2008/03/05/running-selenium-headless/

Solution 6 - Python

The default browser of PhantomJS is IE, though many browser features do not work there. If you want to open a headless(hidden) Firefox window, you can use the new feature of Firefox 56+.

With this feature you can get a headless driver like this:

System.setProperty("webdriver.gecko.driver", firefoxDriverExePath);
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
FirefoxDriver driver = new FirefoxDriver(options);

New versions of Chrome also have the headless option.

Solution 7 - Python

just add these and it will work if you are using chrome, also useful in firefox

from selenium.webdriver.chrome.options import Options
'''option to make driver work background'''
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

Solution 8 - Python

If you're using Selenium RC or Remote WebDriver then you can run the browser instance on a remote, or virtual machine. This means that you shouldn't have to worry about hiding the browser windows as they won't be launching on your local machine.

Solution 9 - Python

Firefox has a headless mode. If you want to use it, you just have to set it on binary options like this:

binary = FirefoxBinary("C:/Program Files/Mozilla Firefox/firefox.exe")
options = webdriver.FirefoxOptions()
# set headless mode on
options.set_headless(True) 
driver = webdriver.Firefox(firefox_binary=binary,options=options)

Solution 10 - Python

If you are using KDE Desktop, you can make Firefox Windows to be initially opened being minimized. That made my day to me regarding this problem. Just do the following:

  1. Open Firefox
  2. Click on the Firefox icon on the top left corner of the menu bar -> Advanced -> Special Application Settings...
  3. Go to the "Size & Position" tab.
  4. Click on "Minimized" and choose "Apply Initially" (YES).

These settings will apply for new Firefox windows from now on and you will not be bothered with pop-ups anymore when running tests with Webdriver.

Solution 11 - Python

Java

I had a similar problem with ChromeDriver (I needed to minimize the browser window while the tests are running). I could not find a better way to do it, so I ended up using the keyboard combination Alt+Space, N to do it. This should work only in Windows, the example uses the Java AWT Robot class to play the keyboard shortcuts:

//Alt + Space to open the window menu
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(200);

// miNimize
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);

Solution 12 - Python

I found the easiest way was to use PhantomJS, per Stéphane's suggestion. I downloaded the binary and put phantomjs in my PATH, in my case (Mac OS) in /usr/bin/. I like to retain the option of seeing what's going on so I wrapped it like this (in Python):

def new_driver():
    if 'VISIBLE_WEBDRIVER' in os.environ:
        return webdriver.Firefox()
    else:
        return webdriver.PhantomJS()

References:
http://blog.likewise.org/2013/04/webdriver-testing-with-python-and-ghostdriver/
http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/

Solution 13 - Python

In Java, you can use HtmlUnitDriver to launch a headless browser session which will not actually open the browser.

Add the following dependency to your pom.xml (or download and reference the following):

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.15</version>
</dependency>

... and test it it as you would a WebDriver driver instance:

 driver = new HtmlUnitDriver();
 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 driver.get("http://www.google.com");
 // etc..
 driver.quit();

Another similar question in SO: https://stackoverflow.com/questions/16911614/avoid-opening-browser-on-remote-server-during-selenium-call

Solution 14 - Python

in options (Firefox options, chrome options )

set boolean headless to true by calling set_headless method.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOverrockSTARView Question on Stackoverflow
Solution 1 - PythonStéphane BruckertView Answer on Stackoverflow
Solution 2 - PythonHarriet.OView Answer on Stackoverflow
Solution 3 - PythonOverrockSTARView Answer on Stackoverflow
Solution 4 - PythonJohnyView Answer on Stackoverflow
Solution 5 - PythonNickolay KondratenkoView Answer on Stackoverflow
Solution 6 - PythonIT manView Answer on Stackoverflow
Solution 7 - PythonshinjiView Answer on Stackoverflow
Solution 8 - PythonDave HuntView Answer on Stackoverflow
Solution 9 - PythonMSSView Answer on Stackoverflow
Solution 10 - PythonClint EastwoodView Answer on Stackoverflow
Solution 11 - PythonvanangelovView Answer on Stackoverflow
Solution 12 - PythonAdam WildavskyView Answer on Stackoverflow
Solution 13 - PythonFuzzy AnalysisView Answer on Stackoverflow
Solution 14 - PythonrinjanView Answer on Stackoverflow