AFETTOUCHE Massinissa
AFETTOUCHE Massinissa

Reputation: 11

How to keep the chrome browser open with Selenium in Python?

I want to open google page with a python code in visual studio editor using the following code :

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

#browser = webdriver.Chrome(ChromeDriverManager().install())
Path = "C:\Program Files (x86)\chromedriver.exe"
browser = webdriver.Chrome(Path)
browser.get('https://www.google.com/')

My problem is the chrome window it opens for 2 or 3 secondes and it close. Did some one face this issue ?

Upvotes: 0

Views: 783

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

The default framework of visual studio editor would close the Chrome browser, per say any browser client after the last line of your program gets executed.

Hence, once the line of code:

browser.get('https://www.google.com/')

gets executed, the Chrome browser is CLOSED. This is the expected behavior.


Incase you like to keep the Chrome browser OPEN even after the last line of your program gets executed, you can pass the experimental option detach as true as follows:

compatible code

from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("detach", True)
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')

Upvotes: 1

Related Questions