Home > Back-end >  Why is my page closing by itself when my program is finished?
Why is my page closing by itself when my program is finished?

Time:11-05

I've made a program that automatically logs into Instagram. Everything works fine, except for the fact that the page randomly closes by itself when the program is finished. Why is that?

My code:

# Importing

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support import ui
from selenium.webdriver.support.ui import WebDriverWait

# Setting everything up

service = Service("\Program Files (x86)\chromedriver.exe")

# Asking Questions

RandTimeInvalid = True
while RandTimeInvalid:
    try:
        RandomnessTime = int(input("Select Randomness Time:"))
        RandTimeInvalid = False
    except ValueError:
        print("'Randomness Time' must be a number!")
        RandTimeInvalid = True
InstagramUsername = input("Enter Username:")
InstagramPassword = input("Enter Password:")

# Logging In

driver = webdriver.Chrome(service=service)
driver.get('https://www.instagram.com/')
wait = ui.WebDriverWait(driver, 10)
driver.maximize_window()
wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.bIiDR"))).click()
RandomnessTime = WebDriverWait(driver, (1 - RandomnessTime))
LoginUsername = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "username"))).send_keys(
    InstagramUsername)
LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(
    InstagramPassword, Keys.ENTER)
wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".sqdOP.L3NKy.y3zKF"))).click()
wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
'''page = requests.get('https://www.instagram.com')
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.select("span")
print(links)
'''

CodePudding user response:

If you want chrome stay open you can try add this

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