Home > OS >  Trying to open url in google chrome, but closes immediately after being launched with selenium
Trying to open url in google chrome, but closes immediately after being launched with selenium

Time:07-03

I tried to launch chrome with selenium. but as soon as the browser loads the urls, google chrome closes automatically. here is the code:

'''

from selenium import webdriver
url= 'https://www.gmail.com'
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)

'''

CodePudding user response:

Because selenium4 no need to use driver.close() method they automatically close the driver after execution. So options.add_experimental_option("detach", True) argument will help you to prevent to close immediately after once launch the url with google chrome

# selenium 4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")

#chrome to stay open
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url= 'https://www.gmail.com'
driver.get(url)
time.sleep(2)

CodePudding user response:

I tried the following code instead. I dont know why it worked this time. I just changed the code for loading chrome driver location. can anyone please explain why it worked now and what was the problem before. '''

from selenium import webdriver
url= 'https://www.gmail.com'
# from webdriver_manager.chrome import ChromeDriverManager
# driver = webdriver.Chrome(ChromeDriverManager().install())
chrome_driver_location ='chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_location)
driver.get(url)

'''

  • Related