0

After my script is executed, and finished, the Chrome window closes. I want the Chrome window to stay open at the end of the script.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
import keyboard

# Datei mit Anmeldedaten (pro Zeile: Benutzername:Passwort)
credentials_file = "anmeldedaten.txt"

# URL für die Anmeldung
login_url = "https://spotify.com/en/login"

# URL der Webseite, auf der der Song abgespielt werden soll
webpage_url = "https://open.spotify.com/intl-de/track/2QyxM5FQLfIApmfeNikEli?si=4fee9599f1da45cd"

# Lade die Anmeldedaten aus der Datei
with open(credentials_file, "r") as file:
    username, password = file.readline().strip().split(":")

    time.sleep(2)

# Funktion zur Anmeldung auf der Webseite
def login(driver):
    driver.get(login_url)
    # Füllen Sie hier die Anmeldedaten aus und klicken Sie auf den Anmelde-Button

    time.sleep(4)

    username_input = driver.find_element(By.CSS_SELECTOR, "input#login-username")
    password_input = driver.find_element(By.CSS_SELECTOR, "input#login-password")

    username_input.send_keys(username)
    password_input.send_keys(password)

    driver.find_element(By.CSS_SELECTOR, "button[data-testid='login-button']").click()

    driver.maximize_window()

    time.sleep(3)

# Funktion zum Öffnen des Songs und Abspielen
def open_and_play_song(driver):
    driver.get(webpage_url)
    wait = WebDriverWait(driver, 10)  # Wartezeit von 10 Sekunden
    keyboard.press_and_release('esc')

    time.sleep(5)

    try:
        cookie = driver.find_element(By.XPATH, "//button[text()='Cookies akzeptieren']")
        cookie.click()
    except NoSuchElementException:
        try:
            button = driver.find_element(By.XPATH, "//button[contains(@class,'onetrust-close-btn-handler onetrust-close-btn-ui')]")
            button.click()
        except NoSuchElementException:
            time.sleep(delay2)
    
    time.sleep(4)
    
    # Hier könnten Sie den Abspiel-Button lokalisieren und klicken
    playmusic_xpath = "(//button[@data-testid='play-button']//span)[3]"
    playmusic = driver.find_element(By.XPATH, playmusic_xpath)
    playmusic.click()

# Chrome-Optionen für den Headless-Modus
chrome_options = Options()
chrome_options.add_argument("--disable-gpu")  # Notwendig, um GPU-Beschleunigung im Headless-Modus zu deaktivieren

# Sitzung erstellen und anmelden
driver = webdriver.Chrome(options=chrome_options)
login(driver)
time.sleep(2)  # Warten Sie, bis die Anmeldung abgeschlossen ist
open_and_play_song(driver)
time.sleep(2)  # Warten Sie, bis die Seite geladen ist`

I want the Chrome window to stay open after my script is executed.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
Luis
  • 11
  • 3
  • Does this answer your question? [Python selenium keep browser open](https://stackoverflow.com/questions/51865300/python-selenium-keep-browser-open) – Shawn Aug 17 '23 at 08:14

2 Answers2

0

Here are two ways to keep the browser open after the script reaches the end.

  1. Detach the browser from chromedriver via options:
chrome_options.add_experimental_option("detach", True)
  1. Add a breakpoint:
import pdb
pdb.set_trace()

or the simplified version of that:

breakpoint()

To continue from the breakpoint, type c and press Enter in the console.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
0
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

include this chrome_option and your job is done.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 19 '23 at 03:59