'Selenium closing the brower alone after finishing all the function

I'm pretty new in programming so I might be an easy question, but I don't understand why the browsers opened by Selenium closes at the end of the code.

from lib2to3.pgen2 import driver

from selenium import webdriver

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('[email protected]')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()

    


Solution 1:[1]

You can use 2 approaches in order to keep you driver open.
1.
Add the 'detach' option to your driver settings:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

Simply add a delay at the end of your test code (less elegant approach but more simple)

from lib2to3.pgen2 import driver

from selenium import webdriver
import time

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('[email protected]')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()

    time.sleep(50)

Solution 2:[2]

This is because after the all functions, The code stops running and that's why selenium exits.

You can use the time module to delay.

import time
from lib2to3.pgen2 import driver

from selenium import webdriver

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('[email protected]')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()
    time.sleep(50) #---> 50 second delay

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2