Stockquotes
Stockquotes

Reputation: 13

Chrome browser closes after running selenium chrome webdriver

I'm currently learning Selenium 4.0 and have set up a basic script that will click a button on Python's website. I'm using a Chrome webdriver. But whenever I run my code, a chrome window opens to the Python website and then closes immediately. How do I keep it open?

The browser version and the webdriver version are the same, and I've even tried the Edge webdriver and reinstalling Chrome. I've even tried downloading a webdriver to my local directory, but that doesn't work either. Here's my current script:

from selenium import webdriver

from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

driver.get("https://www.python.org/")
print(driver.title)
submit = driver.find_element(By.ID, "submit")
submit.click()

After running, my terminal gives this message:

====== WebDriver manager ======
Current google-chrome version is 101.0.4951
Get LATEST chromedriver version for 101.0.4951 google-chrome
Driver [/Users/user1/.wdm/drivers/chromedriver/mac64_m1/101.0.4951.41/chromedriver] found in cache
Welcome to Python.org

Process finished with exit code 0

Upvotes: 1

Views: 6033

Answers (2)

Thomas
Thomas

Reputation: 1354

Solution from @NicoCaldo didn't worked for me.

i've added

while True:
    pass

at the end of my script to keep Chrome open.

Upvotes: 0

NicoCaldo
NicoCaldo

Reputation: 1577

Well, it is the correct behavior as it does everything you told it to do correctly. Infact you're not recieving any errors. After having executed the code, Chrome Driver got killed because the Python app finishes its execution

If you want the Browser opened by the Driver to stay open use Chrome option and add detach

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

Upvotes: 5

Related Questions