Home > Net >  Selenium closing the brower alone after finishing all the function
Selenium closing the brower alone after finishing all the function

Time:01-23

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()

    

CodePudding user response:

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)

CodePudding user response:

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
  •  Tags:  
  • Related