Is it possible to hide the browser in Selenium RC?
Asked Answered
K

13

94

I am using Selenium RC to automate some browser operations but I want the browser to be invisible. Is this possible? How? What about Selenium Grid? Can I hide the Selenium RC window also?

Keister answered 13/9, 2009 at 16:7 Comment(2)
Might not be acceptable, but like couldn't you just move the browser off the screen (using regular windows UI automation?) like place the window at (-10000, -10000)?Burks
Since this question is old now, let me redirect you towards a proper up-to-date answer at the bottom of this page.Apostatize
B
87

There are a few options:

  • You could use Selenium Grid so that the browser is opened on a completely different machine (or virtual machine) that you can then connect to via VNC or Remote Desktop Connection if you wanted to see the browser. Also, another option: if you run a Jenkins foreground process on that remote server, it can execute your test project on the desktop.

  • You can run Selenium 'headless' on Linux in XVFB. I've never tried doing this and doubt it's really worth the effort. http://www.alittlemadness.com/2008/03/05/running-selenium-headless/

  • You can wrap Selenium RC in a Windows service. http://support.microsoft.com/kb/137890 . Except that permissions constraints on later versions of windows will probably prevent Selenium from accessing the desktop like Windows 2000 used to allow us to do.

  • Another option would be to use something like WebDriver HTMLUnitDriver, which doesn't launch a 'real' browser. http://code.google.com/p/webdriver/ . Also there is a PhantomJS option as well as a 'headless Chrome' that you could use.

  • Of course there's also the option of using a service like SauceLabs, where you can get your tests to be run in the cloud. After your tests have completed you can watch a video of them running.

Boarding answered 13/9, 2009 at 18:4 Comment(1)
Here are instructions on creating a bat file to install the service: brantleytec.blogspot.com/2012/11/…Dimercaprol
F
59

On Linux, you can run WebDriver in a headless (virtual) display to hide the browser. This can be done with Xvfb (X virtual framebuffer).

You can control Xvfb directly from Python code using xvfbwrapper: https://github.com/cgoldberg/xvfbwrapper

Python code for running headless would look like this:

from selenium import webdriver
from xvfbwrapper import Xvfb

display = Xvfb()
display.start()

# now Firefox will run in a virtual display. 
# you will not see the browser.
driver = webdriver.Firefox()
driver.get('http://www.google.com')

print(driver.title)
driver.quit()

display.stop()

Install dependencies on Debian/Ubuntu:

$ sudo apt-get install xvfb
$ pip install xvfbwrapper
Flunkey answered 18/1, 2012 at 12:48 Comment(2)
When the code runs in a virtual browser then can I continue using my PC normally while the browser interaction script runs?Rickety
The resources are taken, your machine will become slower!Moil
C
24

I easily managed to hide the browser window.

Just 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.

Changchun answered 27/5, 2014 at 20:11 Comment(4)
It is worth mentioning that you have to put the path of the PhantomJs.exe in order for this to work. example driver = webdriver.PhantomJS(r"C:\\phantomjs\phantomjs.exe") Bromide
Yes, but only on Windows.Apostatize
you don't need to specify the location of the binary as long as it can be found from your environment's $PATH environment variable.Flunkey
PhantomJS is no longer being developed, but Chrome has a headless optionGonfalon
C
14

+1 for Selenium RC as a windows service.

For having the tests run completely hidden, I think you don't have much solutions if you're on windows.

What I'd do it to dedicate a computer in your LAN to be online all the time and have a selenium RC server running. So you use that computer's IP instead of localhost to run your tests. For example:

browser = selenium("10.15.12.34",4444,"*firefox","http://saucelabs.com")

(considering that that's the ip of the computer running the server).

Having that setup, you run your tests in you computer, the browsers and the RC server window are in another computer and the go back to yours once done.

Creuse answered 14/9, 2009 at 4:28 Comment(0)
C
12

On Linux, you can run your test browser on a virtual display. You will need the xvfb package for creating a virtual X server. On Debian based distros, just run

sudo apt-get install xvfb

There is a nice tool ephemeral-x.sh that will conveniently set up any command to run on the virtual display. Download it and make it executable:

wget https://raw.github.com/jordansissel/xdotool/master/t/ephemeral-x.sh
chmod +x ephemeral-x.sh

Then you can simply use it to start the Selenium server:

./ephemeral-x.sh java -jar selenium-standalone.jar

All browser windows created by Selenium will now use the virtual display and will be invisible to you.

Chita answered 4/1, 2013 at 10:50 Comment(1)
This script has a quoting issue. If I say ./ephemeral-x.sh foo '*' it says "Running: foo" followed by the contents of my directory.Gillam
G
4

If you're on Windows, one option is to run the tests under a different user account. This means the browser and java server will not be visible to your own account.

Glomerate answered 17/11, 2009 at 18:16 Comment(0)
A
3

This is how I run my tests with maven on a linux desktop (Ubuntu). I got fed up not being able to work with the firefox webdriver always taking focus.

I installed xvfb

xvfb-run -a mvn clean install

Thats it

Aoudad answered 29/6, 2012 at 12:0 Comment(1)
This is frequently the easiest way to ensure xvfb is running and used by Selenium/WebDriver.Sympathize
D
1

In many cases PhantomJS will not completely suit your needs, I would like to elaborate on the headless chrome option mentioned in Dave Hunt's answer.

chrome 57 has just launched this feature. You can use it by passing the --headless flag via ChromeDriver, for more info see the discussion in this question

Disinclination answered 21/4, 2017 at 13:37 Comment(0)
M
0

There is a PhantomJS related project called GhostDriver , that is meant to run PhantomJS instances in a Selenium Grid using webdriver wire JSON protocol. That is probably what you are looking for, although this question is 4 years old now.

Moose answered 3/4, 2014 at 19:17 Comment(0)
W
0

On MacOSX, I haven't been able to hide the browser window, but at least I figured out how to move it to a different display so it doesn't disrupt my workflow so much. While Firefox is running tests, just control-click its icon in the dock, select Options, and Assign to Display 2.

Wouldst answered 9/7, 2014 at 19:43 Comment(0)
P
0
curl -k https://gist.githubusercontent.com/terrancesnyder/995250/raw/cdd1f52353bb614a5a016c2e8e77a2afb718f3c3/ephemeral-x.sh -o ~/ephemeral-x.sh
chmod +x ~/ephemeral-x.sh
~/ephemeral-x.sh TestsStarterCommand

By the way this is a feature needed by any developer running e2e that logically will spawn browsers. In a development environment it is annoying to deal with the window that keeps popping up and which which you can accidentally interact making the test fail.

Polinski answered 4/8, 2016 at 13:32 Comment(0)
B
0

Using headless Chrome would be your best bet, or you could post directly to the site to interact with it, which would save a lot of compute power for other things/processes. I use this when testing out web automation bots that search for shoes on multiple sites using cpu heavy elements, the more power you save, and the simpler your program is, the easier it is to run multiple processes at a time with muhc greater speed and reliability.

Buckels answered 2/4, 2019 at 23:33 Comment(0)
A
0

To run Selenium with Chrome in headless mode and ensure it uses a desktop view (or it might use mobile browser), set the window size in your ChromeOptions:

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')  # Use desktop size
driver = webdriver.Chrome(options=options)
Alkene answered 4/11, 2023 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.