Reputation: 546
So my understanding is that calling driver.quit or close is the the proper way to close the driver and associated window.
However when running my tests, it seems that even without calling driver.quit and instead calling pass, that the window still closes.
I'm using python with unittest test cases excuted via pytest. I've also run standard unitests via Pycharm. In all scenarios, the browser closes as described. I want the browser to stay open to where I can debug tests. I could just call sleep(9000) but that seems dumb.
Furthermore, the browser stays open when commenting out quit on some machines, but not on others with the same chromedriver, Chrome version, and code.
Analyzing the chromedriver logs, it seems it registers a QuitAll command, but I have no idea where it could be getting it from. Could the pyc file not be overwritten?
Code for quit:
def tearDown(self):
pass
# self.driver.quit()
Upvotes: 9
Views: 15542
Reputation: 15381
For maximizing your debugging options with pytest
and WebDriver, here's some useful info:
pytest comes with some powerful debugging abilities via the interactive Python debugger (“Debug Mode”), which utilizes the following two libraries: the included pdb
library, and the improved (but optional) ipdb
library.
Debug Mode in pytest
can be triggered in a few different ways:
--pdb
option to pytest
.--trace
option to pytest
.pdb.set_trace()
or ipdb.set_trace()
from your test after importing pdb
or ipdb
respectively. (Python versions 3.7+ and newer can also use the breakpoint()
method to enter Debug Mode.)When Debug Mode is activated, the browser window will remain open, and you can see how variables look from the command-line.
(Note that you may need to add -s
to your pytest
run command to allow breakpoints, unless you already have a pytest.ini
file present with addops = --capture=no
in it.)
Once you’re in Debug Mode, there are several commands that you can use in order to control and debug tests:
Additionally, you can execute any Python code that you want from within Debug Mode.
Not all Debug Modes are the same. If you enter Debug Mode after a test raises an exception, you are in the special “Post Mortem Debug Mode”, which lets you explore the stack of the current stack trace, but you can no longer advance to the next line of the method that you were in because that would end the test immediately.
(This is from the pytest Debug Mode tutorial that I created here: https://seleniumbase.com/the-ultimate-pytest-debugging-guide-2021/)
Upvotes: 0
Reputation: 2563
The service will stop once your script ends due to the code here.
If you want chrome and chromedriver to stay open afterward, you can add the detach
option when starting chromedriver:
from selenium.webdriver import ChromeOptions, Chrome
opts = ChromeOptions()
opts.add_experimental_option("detach", True)
driver = Chrome(chrome_options=opts)
Upvotes: 20