Search code examples
python-3.xselenium-webdriverwebdriver

Why is my Selenium web browser closing automatically and how do I fix it?


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.


Solution

  • 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")