0

I am testing my first program for running selenium using python in Chrome version 92

My code at the moment is:

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

options = Options()
options.add_experimental_option("detach", True)
options.add_argument("start-maximized")
options.add_argument("--auto-open-devtools-for-tabs")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

driver = webdriver.Chrome(
    options=options,
    executable_path=r"./chromedriver.exe",
)
driver.get("https://www.python.org")
print(driver.title)

After I run this code, it automatically closes the browser which I want it to remain open. The Chrome driver I am using is for my Chrome version 92 if that helps in any way.

Is there any code I am missing here?

EDIT: The terminal output by the way is:

DevTools listening on ws://127.0.0.1:55201/devtools/browser/829b2f58-670c-4d08-a725-193117104c2f
Welcome to Python.org
KiritoLyn
  • 626
  • 1
  • 10
  • 26
  • 1
    It is expected. Browser is closing because there is nothing to do after printing `driver title` What do you want to do after printing title? – Nandan A Aug 22 '21 at 16:46
  • If it's possible to keep the browser open even after printing title. Or the behavior simply closes if there is nothing left to do? – KiritoLyn Aug 22 '21 at 16:48
  • Does this answer your question? [Python selenium keep browser open](https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open) – Sven Eberth Aug 22 '21 at 16:50
  • I was actually from that question before creating this question. I already tried the detach and global browser answer there, still, the browser closes automatically, I am hoping it to keep open and close manually. – KiritoLyn Aug 22 '21 at 16:54
  • Without `detach` code closing browser is expected, Can you add the code for `detach` part? – Nandan A Aug 22 '21 at 17:16
  • Already did, is it not this one? `options.add_experimental_option("detach", True)` – KiritoLyn Aug 22 '21 at 17:19
  • just put some sleep for few seconds at the end and see if that works – cruisepandey Aug 22 '21 at 17:44
  • 1
    Doesn't sleep only keeps the window open for a certain period of time? – KiritoLyn Aug 22 '21 at 23:34
  • 2
    If you want to keep it open just to check it, you could add a simple `input("Press enter to exit")` statement, that will prevent it from closing indefinitely until you press enter in the shell. – Lucan Aug 25 '21 at 15:26
  • I might do this for now as it at least attains the desired outcome – KiritoLyn Aug 31 '21 at 01:13

0 Answers0