detatch option using chromedriver in Python

1.9k Views Asked by At

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?

2

There are 2 best solutions below

0
undetected Selenium On BEST ANSWER

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)
0
cchinchoy On

"undetected Selenium" is correct to a point, however the way it is exampled, does not give a sufficient view of what can be accomplished or how it can work, the below code shows a better representation of ow to accomplish this as well as the added bonus of the new format to get the Selenium chrome driver to function in the latest version of Selenium. (As of this writing it is version 4.5.0). The below code is tested and works: Code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('detach', True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

driver.get("https://www.google.com/")

Hope this helps......

Regards

Colin