I want to write a Python script using Selenium and Chrome where Selenium won’t close the Chrome browser when the script finishes. From doing a bunch of googling, it looks like the standard solution is to use the detach option. But when I run the following script:
import selenium from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_experimental_option("detach", True) driver = webdriver.Chrome(options=chrome_options) driver.get("https://www.google.com/")
It opens up Chrome, goes to Google’s homepage, and then closes the browser. It’s not throwing any errors.
Any idea why it’s not working? I’m using the latest version of Google Chrome on Windows 10, and I’ve got the latest version of the selenium module installed. I couldn’t find anything online that said the experimental detach option no longer existed. And I double checked the API, and it looks like it’s the right syntax.
Advertisement
Answer
This code worked perfectly for me using : selenium-wire
, hope it works for you
from seleniumwire import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_experimental_option("detach", True) driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options) driver.get("https://www.google.com/")