'Is there a way to hide the browser while running selenium in Python?

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



Solution 1:[1]

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)

Solution 2:[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.

Solution 3:[3]

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:

Solution 4:[4]

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

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Meshi
Solution 2 Aiyion.Prime
Solution 3
Solution 4 ezzeddin
Solution 5 akash pawar