S
S
Sergey Lutchenko2017-01-13 02:00:35
Python
Sergey Lutchenko, 2017-01-13 02:00:35

How to launch web driver chrome without opening a browser window (i.e. in the background)?

Hey!
When writing the parser, I used Selenium with webbriver Chrome. Everything works fine, but at startup it opens a browser window. The code is the following:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import datetime, timedelta
from pyvirtualdisplay import Display
from selenium.webdriver.chrome.options import Options

browser = webdriver.Chrome(chrome_options=chrome_options) 
browser.get('https://www.itau.com.br')

To run it in the background, I read that you need to run a virtual display, did:
display = Display(visible=0, size=(800, 600))
display.start()

But the Chrome window still pops out! Found that you can add the option "do not open the window", added:
chrome_options = Options()
chrome_options.add_argument("--no-startup-window")

This is where the error started popping up:
browser = webdriver.Chrome(chrome_options=chrome_options) 
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/Users/Lutchenko/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.12.2 x86_64)

Any idea why it pops up or some other way to hide the window or run it in the background?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Lutchenko, 2017-01-14
@baushtelle

In general, having rummaged fairly on the Internet, I realized that, for now, it is impossible to run Chrome with Selenium in headless mode (that is, in the background) on Mac OS , but soon such an opportunity will appear. Same story with Firefox. I put Ubuntu for tests.

M
Mintormo, 2017-01-13
@Mintormo

In general, Selenium seems to be more for testing. Check out Scrapy .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question