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
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()
def createSession():
**global driver**
driver = webdriver.Chrome(chrome_driver_path)
driver.maximize_window()
driver.get("https://google.com")
return driver
My solution is to define the driver in the init function first, then it won't close up the browser even the actional
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
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 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()