selenium-webdriverselenium-chromedriverselenium4

The browser window controlled by WebDriver closes automatically after opening


Here is my code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

PATH = r"D:\vscode_project\webdriver\chromedriver-win64\chromedriver.exe"
service = Service(PATH)

driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")

print(driver.title)

I want the browser window controlled by the WebDriver to remain on the screen after running the program, rather than closing automatically after execution (But I'm not sure if this is how Selenium 4 is set up by default).

I did not add driver.quit(), but it still closes automatically after displaying the website title, instead of staying on the Google.com page. Is this correct?

Python version: 3.12.3

Selenium version: 4.31.1


Solution

  • Try this

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options
    
    
    service = Service(ChromeDriverManager().install())
    chrome_options = Options()
    chrome_options.add_experimental_option("detach", True)
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.get("https://www.google.com")
    print(driver.title)