Home > Software design >  How to stop browser closing in python selenium? without calling quit or close ()
How to stop browser closing in python selenium? without calling quit or close ()

Time:11-17

Description of the problem: The problem I'm stuck on is when I run a code, it first opens the chrome browser and opens the google.com website and then it closes it for no reason. This is my code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
opts = Options()
opts.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver_service = Service(executable_path=PATH)
driver = webdriver.Chrome(service=driver_service,options=opts)
driver.get("https://www.google.com/")
print("connected to: "   driver.title)

What I expect is: that my bot will stay on google.com and not close it! For example: according to a video https://www.youtube.com/watch?v=Xjv1sY630Uc at minute 9:37 he runs his bot, a bot that does not close the window and compared to what my bot does to me it does close the window (I don't understand why it opens and immediately closes the window with my bot)

Here we see that my program finished without errors but also see that it stopped working for no reason

CodePudding user response:

Add this code and try:

options = Options()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=driver_service,options=options)

Don't forget to add double slash '\\' in the chromedriver.exe path.

  • Related