How to hide Firefox window (Selenium WebDriver)?

101,439

Solution 1

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 2

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)?

Solution 3

Just add the following code.

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

Solution 4

Just do (Python):

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

Solution 5

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/

Share:
101,439
Paresh
Author by

Paresh

Updated on June 23, 2020

Comments

  • Paresh
    Paresh about 4 years

    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.

  • Paresh
    Paresh over 13 years
    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
    Paresh over 13 years
    So there is really now other solution to hide Firefox? :(.. HtmlUnitDriver can help but i don't want to use it.
  • Darren Ringer
    Darren Ringer over 9 years
    'rest of your code won't need to be changed' - unless, of course, it relies specifically on the Firefox browser and its configuration options.
  • David Röthlisberger
    David Röthlisberger over 9 years
    With xvfb-run (part of the xvfb package, on Ubuntu at least) it's even easier: xvfb-run firefox.
  • 110SidedHexagon
    110SidedHexagon over 8 years
    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.
  • Paresh
    Paresh over 8 years
    I don't know what you are trying to say! didn't get you, Sorry!
  • 110SidedHexagon
    110SidedHexagon over 8 years
    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.
  • Tejesh Raut
    Tejesh Raut over 8 years
    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 ?
  • Stéphane Bruckert
    Stéphane Bruckert over 8 years
    @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.
  • Anko
    Anko over 8 years
    -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.
  • ivan_pozdeev
    ivan_pozdeev over 6 years
  • Lal
    Lal over 6 years
    Update: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  • dequis
    dequis about 5 years
    As another answer points out, this is a new feature since firefox 56
  • Felipe Valdes
    Felipe Valdes almost 5 years
    I came here to say the same as @LalZada, this (even though it works) is no longer the recommended path.