5

I am working on a project with selenium to scrape the data, but I don't want the browser to open and pop up. I just wanted to hide the browser and also not to display it in the taskbar also...

Some also suggested to use phantomJS but I didn't get them. What to do now ...

Abhay Salvi
  • 890
  • 3
  • 15
  • 39

5 Answers5

14

If you're using Chrome you can just set the headless argument like so:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

driver_exe = 'chromedriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(driver_exe, options=options)
Meshi
  • 470
  • 4
  • 16
4

To hide the browser while executing tests using Selenium's you can use the minimize_window() method which eventually minimizes/pushes the Chrome Browsing Context effectively to the background using the following solution:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()

Alternative

As an alternative you can use the headless attribute to configure ChromeDriver to initiate browser in Headless mode using Selenium and you can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Well it doesn't hide the window... It just minimizes it. – Abhay Salvi Mar 05 '20 at 10:53
  • 1
    @AndrewWhiteman Still that should serve your purpose of _...hide the browser..._, however I have updated the answer, let me know the status. – undetected Selenium Mar 05 '20 at 11:01
  • @Abhaysalvi: "It just minimizes it". Yes, and it's exactly what I was looking for. This way, the browser window doesn't disturb me when it opens, but I can still access it when I want to. – Eric Duminil Jun 08 '21 at 08:46
2

For chrome you could pass in the --headless parameter.

Alternatively you could let selenium work on a virtual display like this:

from selenium import webdriver
from xvfbwrapper import Xvfb

display = Xvfb()
display.start()

driver = webdriver.Chrome()
driver.get('http://www.stackoverflow.com')

print(driver.title)
driver.quit()

display.stop()

The latter has worked for me quite well.

Aiyion.Prime
  • 973
  • 9
  • 20
0

If you're using Firefox, try this:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

driver_exe = 'path/to/firefoxdriver'
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(driver_exe, options=options)

similar to what @Meshi answered in case of Chrome

ezzeddin
  • 499
  • 2
  • 5
  • 25
0

if you want to hide chrome or selenium driver there is a library pyautogui

import pyautogui

window = [ x for x in pyautogui.getAllWindows()]

by this, you are getting all window title now you need to find your window

for i in window:
    if 'Google Chrome' in i.title:
        i.hide()

or you can play with your driver title also