NoahsarkTaiwan
NoahsarkTaiwan

Reputation: 3

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

Upvotes: 0

Views: 100

Answers (2)

user27016283
user27016283

Reputation: 1

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
import code

PATH = r"C:\Program Files (x86)\chromedriver.exe"

service = ChromeService(executable_path=PATH)
driver = webdriver.Chrome(service=service)

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

print(driver.title)

code.interact(local=locals())

driver.quit()

Upvotes: 0

SSujitXX
SSujitXX

Reputation: 333

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)

Upvotes: 0

Related Questions