I am trying to keep the chrome browser open after selenium finishes executing my test script. I want to re-use the same window for my second script to run.

- 183,867
- 41
- 278
- 352

- 31
- 1
- 1
- 3
-
It's a best practice to use a new browser for each test to ensure a clean run each time. If that's not your requirement, you will need to post the relevant code so we can see how to help you. – JeffC Feb 04 '17 at 20:47
3 Answers
Browser window closes when your Chrome webdriver instance variable is garbage collected. If you want to avoid this even when your script finishes executing, you can make it global. I.e.:
def test():
global driver # this will prevent the driver variable from being garbage collected
driver = webdriver.Chrome()
...
Explanation:
A selenium.webdriver.Chrome
class instance contains an instance of a Service
class. The latter has a __del__
method which is called when the instance is being destructed during garbage collection process. The method in turn stops the service and causes Chrome browser window to close.
This also explains why some users don't observe this behavior. I suspect that this is because they have Chrome webdriver instance variable at file scope, not inside a function.

- 6,659
- 4
- 44
- 53
I know what to do in WATIR(Ruby language), I am writing the code below, So it might give you the clue what to do with your language
require 'watir'
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: {detach: true})
b = Watir::Browser.new :chrome, desired_capabilities: caps
b.goto('www.google.co.uk')
This given below line is important, If you can re-write this line your language(python),then you may prevent from closing chrome browser
caps = Selenium::WebDriver::Remote::Capabilities.chrome(chrome_options: {detach: true})

- 898
- 7
- 16
This should be as simple as not calling driver.quit() at the end of your test case. You should be left with the chrome window in an opened state.

- 133
- 1
- 7
-
i have removed driver.quit() still it does close the browser after script ends. – Nitesh Patel Feb 04 '17 at 20:13
-
-
1i used the remote web driver and now it works perfectly thanks for help. – Nitesh Patel Feb 05 '17 at 01:07
-