Google chrome closes immediately after being launched with selenium

Viewed 45711

I am on Mac OS X using selenium with python 3.6.3.

This code runs fine, opens google chrome and chrome stays open.:

chrome_options = Options()
chrome_options.binary_location="../Google Chrome"
chrome_options.add_argument("disable-infobars");
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get("http://www.google.com/")

But with the code wrapped inside a function, the browser terminates immediately after opening the page:

def launchBrowser():
   chrome_options = Options()
   chrome_options.binary_location="../Google Chrome"
   chrome_options.add_argument("disable-infobars");
   driver = webdriver.Chrome(chrome_options=chrome_options)

   driver.get("http://www.google.com/")
launchBrowser()

I want to use the same code inside a function while keeping the browser open.

11 Answers

My guess is that the driver gets garbage collected, in C++ the objects inside a function (or class) get destroyed when out of context. Python doesn´t work quite the same way but its a garbage collected language. Objects will be collected once they are no longer referenced.

To solve your problem you could pass the object reference as an argument, or return it.

    def launchBrowser():
       chrome_options = Options()
       chrome_options.binary_location="../Google Chrome"
       chrome_options.add_argument("start-maximized");
       driver = webdriver.Chrome(chrome_options=chrome_options)

       driver.get("http://www.google.com/")
       return driver
    driver = launchBrowser()

Just simply add:

while(True):
    pass

To the end of your function. It will be like this:

def launchBrowser():
   chrome_options = Options()
   chrome_options.binary_location="../Google Chrome"
   chrome_options.add_argument("disable-infobars");
   driver = webdriver.Chrome(chrome_options=chrome_options)

   driver.get("http://www.google.com/")
   while(True):
       pass
launchBrowser()

The browser is automatically disposed once the variable of the driver is out of scope. So, to avoid quitting the browser, you need to set the instance of the driver on a global variable:

Dim driver As New ChromeDriver

Private Sub Use_Chrome()

driver.Get "https://www.google.com"
'  driver.Quit
End Sub

To make the borwser stay open I am doing this:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def browser_function():
    driver_path = "path/to/chromedriver"
    chr_options = Options()
    chr_options.add_experimental_option("detach", True)
    chr_driver = webdriver.Chrome(driver_path, options=chr_options)
    chr_driver.get("https://target_website.com")

This is somewhat old, but the answers on here didn't solve the issue. A little googling got me here

http://chromedriver.chromium.org/getting-started

The test code here uses sleep to keep the browser open. I'm not sure if there are better options, so I will update this as I learn.

import time
from selenium import webdriver

    driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
    driver.get('http://www.google.com/xhtml');
    time.sleep(5) # Let the user actually see something!
    search_box = driver.find_element_by_name('q')
    search_box.send_keys('ChromeDriver')
    search_box.submit()
    time.sleep(5) # Let the user actually see something!
    driver.quit() 

My solution is to define the driver in the init function first, then it won't close up the browser even the actional

To whom has the same issue, just set the driver to global, simple as following:

global driver
driver.get("http://www.google.com/")

This resolved the problem on my side, hope this could be helpful

As per the code block you shared there can be 3 possible solutions as follows :

  • In the binary_location you have to change the .. to . (. denotes Project Workspace)
  • In the binary_location you have to change the .. to /myspace/chrome (Absolute Chrome Binary)
  • While initiating the driver, add the switch executable_path :

    driver = webdriver.Chrome(chrome_options=options, executable_path=r'/your_path/chromedriver')
    
def createSession():
    **global driver**
    driver = webdriver.Chrome(chrome_driver_path)
    driver.maximize_window()
    driver.get("https://google.com")
    return driver

To prevent this from happening, ensure your driver variable is defined outside your function or as a global variable, to prevent it from being garbage collected immediately after the function completes execution.

In the example above, this could mean something like:

driver = webdriver.Chrome(chrome_options=get_options())

def get_options():
   chrome_options = Options()
   chrome_options.binary_location="../Google Chrome"
   chrome_options.add_argument("disable-infobars")
   return chrome_options

def launchBrowser():
   driver.get("http://www.google.com/")

launchBrowser()

Hi the below solutions worked for me Please have a try these.

Solution 1 : of Google chrome closes automatically after launching using Selenium Web Driver.

Check the Version of the Chrome Browser driver exe file and google chrome version you are having, if the driver is not compatible then selenium-web driver browser terminates just after selenium-web driver opening, try by matching the driver version according to your chrome version

Related