Search code examples
pythonseleniumgoogle-chromeselenium-chromedriverdetach

detatch option using chromedriver in Python


detatch doesn't seem to work at all. The script I have (when ran in command prompt as opposed to idle) closes the window when done. I thought adding detach as an option would prevent this.

What can I do to prevent this chromedriver window from closing when finished? why doesnt this work?

Code:

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import selenium.webdriver.chrome.options as Options
from webdriver_manager.chrome import ChromeDriverManager

print("leeeeeel 0.1")
chrome_options = Options.Options()
chrome_options.add_argument("detatch")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()) ,options=chrome_options)
driver.get("https://www.google.com")
print("1")

Output in command prompt:

>python "close test.py"
leeeeeel 0.1


====== WebDriver manager ======
Current google-chrome version is 100.0.4896
Get LATEST chromedriver version for 100.0.4896 google-chrome
Driver [C:\Users\zfqaaa\.wdm\drivers\chromedriver\win32\100.0.4896.60\chromedriver.exe] found in cache

DevTools listening on ws://127.0.0.1:54339/devtools/browser/0132a2ac-6783-4485-bbbc-8b8a10178be2
1
[1772:5288:0420/135655.757:ERROR:device_event_log_impl.cc(214)] [13:56:55.757] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[1772:5288:0420/135655.758:ERROR:device_event_log_impl.cc(214)] [13:56:55.759] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[1772:5288:0420/135655.761:ERROR:device_event_log_impl.cc(214)] [13:56:55.761] USB: usb_device_handle_win.cc:1049 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

When I run it in IDE the chrome window stays open with or without detatch added. why does detatch not work?


Solution

  • Instead of adding the argument you need to add an experimental_option as follows:

    import selenium.webdriver.chrome.options as Options
    
    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)