Python Selenium window closing no matter what

Issue

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 selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(options=chrome_options, service=s)
driver.maximize_window()
driver.get('https://www.youtube.com')

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/212826.842:ERROR:device_event_log_impl.cc(214)] [21:28:26.841] USB: usb_device_handle_win.cc:1048 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.

Solution

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.

time.sleep(10)

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