Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Chrome closed itself immediately after filling the form in selenium

I am working with selenium right now the below code works fine but after filling the form my chrome browser closes itself immediately. How do I prevent this from happening. I don’t want the browser to close itself.

The code is below

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import time

time.sleep(5)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)



MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Selenium always automatically quits after a code is finished running. You can add a time.sleep() to keep it open

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)
time.sleep(30)

You can also do this by adding ‘detach’ option, as following:

from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver

from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)

driver.get("https://ssg2021.in/citizenfeedback")

# selcting states
state_select = driver.find_element(By.ID,'State')
drp1 = Select(state_select)

drp1.select_by_visible_text('Chhattisgarh')


# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp2 = Select(district_select)

drp2.select_by_index(10)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading