Is there a way to hide the browser while running selenium in Python?
Asked Answered
S

5

6

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

Sex answered 5/3, 2020 at 10:14 Comment(2)
with selenium, you can set option of browser to headlessWest
Does this answer your question? Running Selenium with Headless Chrome WebdriverBixby
R
15

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)
Ruction answered 5/3, 2020 at 10:53 Comment(0)
M
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:

Miosis answered 5/3, 2020 at 10:28 Comment(3)
Well it doesn't hide the window... It just minimizes it.Sex
@AndrewWhiteman Still that should serve your purpose of ...hide the browser..., however I have updated the answer, let me know the status.Miosis
@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.Element
G
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.

Guttering answered 5/3, 2020 at 10:20 Comment(0)
P
1

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

Polad answered 16/9, 2021 at 22:44 Comment(0)
G
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

Garthgartner answered 11/5, 2022 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.