Home > Back-end >  Browser Close Immediately | Selenium
Browser Close Immediately | Selenium

Time:01-30

I start to learn selenium today. I install chrome driver and firefox driver accoding to version. I try with both chrome and firefox but browsers closes immediately. I also add drivers to PATH. I also try using direct path of driver

Chrome Version: 109.0.5414.120

Selenium Version: 4.8

Python Version: 3.8

Conda Version: 23.1.0

Code:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__)  # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")

CodePudding user response:

Service need to get the path to the chromedriver.exe as following:

service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=service)

C:\webdrivers\chromedriver.exe here is the actual path to chromedriver.exe on my computer

To keep the browser open set options with detach = true, as wollowing:

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

options = Options()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")

service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=service)

CodePudding user response:

from selenium import webdriver

# Selenium Version
print(webdriver.__version__)

# Instantiating the Chrome driver
chrome_browser = webdriver.Chrome()

# Opening the browser to Google website
chrome_browser.get("https://www.google.com")

CodePudding user response:

Seems like it closes, because your code is done running.

You might try:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service(r"C:\Users\Sarper\Drivers\chromedriver.exe")
print(selenium.__version__)  # Verison 4.8
chrome_browser = webdriver.Chrome(service=service)
chrome_browser.get("https://www.google.com")

input("Press ENTER to exit\n")
  • Related