Python Selenium window closing no matter what


I don’t really like to ask questions but I just can’t find out what is wrong with my code. I’m new to selenium so please excuse me if it’s something obvious.

from selenium import webdriver
from import Service
from import ChromeDriverManager
from import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options, service=s)

This code works, and opens up youtube successfully, however, the window will close shortly after opening. To combat this, I added the ‘detach True’ option into the code as shown above (Python selenium keep browser open), however, this hasn’t worked and the window will close a few seconds after opening. There was also this error showing when I ran the code.

[17708:21796:0720/] [21:28:26.841] USB: Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

I looked at other people on SO who had this issue, but all the resources said to ignore it and that it shouldn’t affect the running of the program. To stop the error message from popping up I put this line into my code.
chrome_options.add_experimental_option(‘excludeSwitches’, [‘enable-logging’])
This stopped the error from showing up but didn’t stop the window from closing.

Any help is appreciated, I’m running the most recent version of VS on windows 10.


After your test case finishes running, it will close the browser no matter what. In your case browser will be closed as soon as you navigate to youtube. You don’t have anything else and your test case is finished as soon as you navigate to youtube.

But, if you would like to observe more and stay on youtube once you navigate, you can add wait time so it doesn’t close as soon as it navigates to youtube.

Try adding this line below so that it will wait for 10 seconds.


Answered By – Elliot Ryder

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published