9

I have a python test and I want to run it without opening the firefox

I want to hide it so how can I do this ?

any help ?

1
  • "Python test" is a bit unclear. You mean you want to run a site UI test using selenium? In that case, you can use PhantomJS (with selenium as well), it is a browser running in background without any kind of graphical interface and, obviously, windows. Commented Mar 16, 2017 at 9:10

3 Answers 3

23

Headless:

# pip install selenium
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.add_argument('--headless')

driver = webdriver.Firefox(options=options)
driver.get('https://www.google.com/doodles')

print('Title: "{}"'.format(driver.title))
driver.quit()
Sign up to request clarification or add additional context in comments.

Comments

4

You can try to set negative window position to hide browser:

driver.set_window_position(-3000, 0) # driver.set_window_position(0, 0) to get it back

or try PhantomJS headless browser, that allow to run UI tests without displaying browser window

4 Comments

yes I need to runit backgroud so I prefer to use PhantomJS
how can I use PhantomJS ? should I change python code to use it ? should I import sth ?
Download binary, put it in your Python root folder and define your driver as driver=webdriver.PhantomJS()
no, nothing to import additionally. But note that PhantomJS has a lot of bugs, so it works not so good as Firefox or Chrome...
0

Have a look at this answer: https://stackoverflow.com/a/6300672/2929488

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

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

display.stop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.