46

I am using selenium to open some browser windows for marketing reasons. I simply open my marketing sources, login via selenium and start working.

The problem is, that after the code is executed selenium closes the window.

All solutions havent helped much so far.

I have 13 browser windows atm., which look like this:

def open_instagram():    
    try:
        # Connect
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)
        browser.set_window_size(1800, 900)
        browser.get("https://www.instagram.com/accounts/login/?hl=de")
        browser.find_element(By.NAME, 'username').send_keys('MYEMAIL', Keys.TAB, 'MYPW', Keys.ENTER)
    except Exception as e:
        print (e, 'Instagram')

open_instagram()

The closest solution which I found is adding this at the end of my script, but somehow it will only keep 5 windows open, than close 5 windows and open next 5 new windows:

while True:
    pass

I just want that selenium keeps all browser windows open, until I close them manually.

Roman
  • 3,563
  • 5
  • 48
  • 104

5 Answers5

77

If you want chrome and chromedriver to stay open, you have to use the 'detach' option when starting chromedriver.

In your case add :

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

before you pass in the options to the browser

browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)

Or you can run the code in debug mode with breakpoint at the end and when it pauses 'kill' the program and take over the browser if you want to, but this works in IDE only.

EDIT - added the import for clarity

user985366
  • 1,635
  • 3
  • 18
  • 39
AnkDasCo
  • 1,439
  • 11
  • 16
  • You also need from "selenium.webdriver.chrome.options import Options" – Nisheeth Mar 29 '19 at 01:31
  • 7
    Is it possible to reuse this browser window? E.g. get the PID or something and use explicitly if exists? – Jeppe Jun 27 '19 at 09:32
  • With Python you use `chrome_options.add_experimental_option("detach", True)`, with C# you use the `ChromeOptions.LeaveBrowserOpen` property... either way tho neither of them will function until you add `options.AddExcludedArgument("enable-automation")` to your options setup. – jrypkahauer Dec 05 '19 at 21:11
  • 3
    `("detach", True)` option only works on terminal. It doesn't work when debugging in VSCode – mustafa candan Jul 16 '22 at 10:55
  • 2
    To exclude `enable-automation` (that hides the "Chrome is being controlled by automated software banner" banner, and maybe does other things, but does not actually disable automation) in Python, use `chromeOptions.add_experimental_option("excludeSwitches", ["enable-automation"])` – gluttony Sep 09 '22 at 09:13
  • 'chrome_options' is deprecated, use 'options' instead like so: `browser = webdriver.Chrome('drivers/chromedriver.exe', options=chrome_options)` – digitalguy99 Jul 11 '23 at 03:35
8

You can also add global browser like so:

def open_instagram():    
    try:
        # Connect
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        global browser # this will prevent the browser variable from being garbage collected
        browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)
        browser.set_window_size(1800, 900)
        browser.get("https://www.instagram.com/accounts/login/?hl=de")
        browser.find_element(By.NAME, 'username').send_keys('MYEMAIL', Keys.TAB, 'MYPW', Keys.ENTER)
    except Exception as e:
        print (e, 'Instagram')

open_instagram()

Source

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
2

The simplest way to do this would be just to add input() after your get function. This will prompt you to have to press the Enter key to continue and will thus keep your program from finishing. It might work more considering you seem to be manually shifting through each webpage? Alternatively you can stop it from closing using the time.sleep function in the time module, but that isn't efficient for keeping it open indefinitely.

0

Selenium 4 / PHP / Docker

$this->driver = RemoteWebDriver::createBySessionID(self::$session_id, self::$server, 60000, 60000);

version: "3.5"
#Latest version
networks:
  grid-network:

services:

  selenium-hub:
    image: selenium/hub:latest
    container_name: selenium-hub
    ports:
      - "4446:4444"
    networks:
      - grid-network
      
  chrome:
    shm_size: 4gb 
    image: selenium/standalone-chrome:latest
    container_name: chrome
    depends_on:
      - selenium-hub
    environment:
      - NODE_MAX_SESSION=5
      - NODE_MAX_INSTANCES=5
      - GRID_MAX_SESSION=31556926
      - GRID_BROWSER_TIMEOUT=31556926
      - GRID_TIMEOUT=31556926
      - GRID_SESSION_TIMEOUT=31556926
      - SESSION_TIMEOUT=31556926
      - NODE_SESSION_TIMEOUT=31556926
      - GRID_CLEAN_UP_CYCLE=31556926
      - SE_NODE_SESSION_TIMEOUT=31556926
      - SE_SESSION_REQUEST_TIMEOUT=31556926
    volumes:
      - /dev/shm:/dev/shm
    ports:
      - "33333:5900"
      - "3333:7900"
      - "44444:4444"
    links:
      - selenium-hub
    networks:
      - grid-network
0

detach options is right way to solve your problem

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '23 at 12:01