7

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

2

5 Answers 5

16

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Be aware that the latest version of selenium has changed the order of the webdriver.Chrome() arguments. 'options' now is first, so if you use the code above, Python will correctly complain that you specified multiple values for that parameter. I found that simply using 'Chrome(options)' was fine, without any driver executable, but check the docs if that doesn't work for you.
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:

3 Comments

Well it doesn't hide the window... It just minimizes it.
@AndrewWhiteman Still that should serve your purpose of ...hide the browser..., however I have updated the answer, let me know the status.
@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.
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.

Comments

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

Comments

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

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.