52

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.

duan
  • 8,515
  • 3
  • 48
  • 70
Paresh
  • 1,140
  • 2
  • 12
  • 29
  • Very related (for other browsers): [Can Selenium WebDriver open browser windows silently in the background?](https://stackoverflow.com/questions/16180428/can-selenium-webdriver-open-browser-windows-silently-in-the-background#comments-23447450) -- contains [this answer which uses pyvirtualdisplay, which is not mentioned here](https://stackoverflow.com/a/23447450/5267751). – user202729 Dec 22 '20 at 01:33

14 Answers14

73

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: How to run ghostdriver with Selenium using java


C#

How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • 7
    'rest of your code won't need to be changed' - unless, of course, it relies specifically on the Firefox browser and its configuration options. – Darren Ringer Feb 20 '15 at 14:20
  • My code is not showing the desired result it used to show while using firefox. Also screen.png shows a blank png image just after using driver.get("URL") Do you have any idea ? – Tejesh Raut Nov 21 '15 at 06:46
  • 1
    @TejeshRaut I couldn't tell. It seems like you have a particular issue. I would try to reduce the amount of code needed to reproduce the problem and post a new question. – Stéphane Bruckert Nov 21 '15 at 15:26
  • 6
    -1 The question is specifically about using WebDriver _for Firefox_. PhantomJS is a completely different browser, currently with no support for newer web features like CSS `flexbox` or WebGL. – Anko - inactive in protest Mar 25 '16 at 10:28
  • 1
    [PhantomJS has been abandoned in favor of Headless Chrome(ium)](https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE%5B1-25%5D). – ivan_pozdeev Jan 31 '18 at 02:03
  • 11
    Update: `UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead` – Lal Feb 01 '18 at 08:23
  • I came here to say the same as @LalZada, this (even though it works) is no longer the recommended path. – Felipe Valdes Aug 19 '19 at 10:43
28

Just add the following code.

import os
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()
Harriet.O
  • 419
  • 1
  • 5
  • 8
  • 2
    As another answer points out, this is a new feature [since firefox 56](https://developer.mozilla.org/en-US/Firefox/Headless_mode) – dequis Jun 10 '19 at 12:23
16

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!

ibodi
  • 1,543
  • 3
  • 21
  • 40
Paresh
  • 1,140
  • 2
  • 12
  • 29
  • 2
    One problem with this method though is that the hidden window still takes away focus from whatever you are doing. It doesn't mess up the execution any more, but it makes trying to type very hard. – 110SidedHexagon Nov 06 '15 at 12:40
  • I don't know what you are trying to say! didn't get you, Sorry! – Paresh Nov 09 '15 at 03:53
  • 2
    I used this method to hide a Chrome browser to run a program that downloads files off of a website and compiles the data into an excel form. While the program runs, clicking buttons and filling text boxes in the hidden browser, I noticed it will steal focus from whatever window I am on when it does actions with the web driver. As an example, if the program is running, and I want to type an email, I can start typing but at some point the window will lose focus and I will no longer be typing in the email window, but the hidden browser window instead. – 110SidedHexagon Nov 09 '15 at 13:51
14

Just do (Python):

opts = webdriver.FirefoxOptions()
opts.headless = True
firefox = webdriver.Firefox(options=opts)
Johny
  • 809
  • 9
  • 19
12

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/

Nickolay Kondratenko
  • 1,871
  • 2
  • 21
  • 25
8

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.

IT man
  • 563
  • 6
  • 9
8

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')
shinji
  • 81
  • 1
  • 1
4

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)
ibodi
  • 1,543
  • 3
  • 21
  • 40
MSS
  • 3,520
  • 24
  • 29
4

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.

Dave Hunt
  • 8,191
  • 4
  • 38
  • 39
  • I am not using remote web driver; and my client requirement is to have everything on one machine so they want want to hide browser. There is no way to hide Browser? – Paresh Mar 21 '11 at 08:14
  • So there is really now other solution to hide Firefox? :(.. HtmlUnitDriver can help but i don't want to use it. – Paresh Mar 25 '11 at 20:57
3

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.

KatieK
  • 13,586
  • 17
  • 76
  • 90
Clint Eastwood
  • 4,995
  • 3
  • 31
  • 27
2

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);
Community
  • 1
  • 1
vanangelov
  • 164
  • 2
  • 7
2

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/

savanto
  • 4,470
  • 23
  • 40
Adam Wildavsky
  • 281
  • 2
  • 7
1

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: Avoid opening browser on remote server during selenium call

Community
  • 1
  • 1
Fuzzy Analysis
  • 3,168
  • 2
  • 42
  • 66
1

in options (Firefox options, chrome options )

set boolean headless to true by calling set_headless method.

rinjan
  • 550
  • 5
  • 19