1

Selenium webdriver automatically closes, what am I doing wrong?

I'm new in learning Selenium. I have been trying to get hold of the webpage put it seen to closes itself automatically. I do not understand the problem. I tried many method searched on stack overflow also googled it and read the Documentation of selenium.

from selenium import webdriver

chrome_driver_path = "D:\chromedirver.exe"   
dirver = webdirver.Chrome(executable_path=chrome_driver_path)
driver.get("https://www.google.com")

After some research I changed my code but facing the same problem.

Code after some research

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

chrome_driver_path = "D:\chromedriver.exe"    
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))    
driver.get("https://google.com")

Still B=browser closes automatically.

Shawn
  • 4,064
  • 2
  • 11
  • 23
Saqib
  • 15
  • 6
  • 1
    Check this question, you will get the answer here. [Python selenium keep browser open](https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open) – Shawn May 19 '23 at 15:44

3 Answers3

2

Just add these two lines

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver=webdriver.Chrome(executable_path=chrome_driver_path,options=options)
driver.get("https://www.google.com")
1

The reason why your selenium script is resulting in the browser closing automatically is because it has reached the end of execution. The program exists after execution of the last function i.e. driver.get("https://google.com") completes.

If you want the browser to remain on the screen after execution of the program, you could go ahead with what Mohit Kumar has mentioned above which basically detaches your browser from the python program's execution. Alternatively, you can use explicit waits to look out for certain components on the DOM or use the time.sleep() function to defer the execution of the program (not recommended)

Using time.sleep() :

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://google.com")

sleep(30)

Using Explicit Waits :

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://google.com")

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name=q]")))
Techrookie89
  • 380
  • 3
  • 14
0

Also you cand have only:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service


service_obj = Service("D:/Python/chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service_obj, options=options)
driver.get("https://google.com")