Abhay Salvi
Abhay Salvi

Reputation: 1099

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

Upvotes: 6

Views: 20690

Answers (5)

akash pawar
akash pawar

Reputation: 21

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

Upvotes: 0

ezzeddin
ezzeddin

Reputation: 521

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

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193058

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:

Upvotes: 4

Meshi
Meshi

Reputation: 502

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)

Upvotes: 15

Aiyion.Prime
Aiyion.Prime

Reputation: 1042

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.

Upvotes: 2

Related Questions