Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Instagram login page everytime using Selenium and Python

I am trying to make a selenium instagram script. My issue is I have to login my account every time.

If I use my normal google chrome, only one time login is enough. It's always stay logged in when ı open Chrome. But Chrome driver always aks for login.

from selenium import webdriver
import time
import random
from selenium.webdriver.common.by import By
import kullaniciBilgileri as kb
import os.path
from pathlib import Path
from selenium.webdriver.chrome.options import Options
import collections

options = Options()
options.add_experimental_option('useAutomationExtension', False)
link = "http://www.instagram.org"
browser = webdriver.Chrome(options=options, executable_path="./chromedriver")
browser.get(url=link)
time.sleep(10)
username = browser.find_element(by=By.NAME, value=("username"))
password = browser.find_element(by=By.NAME, value=("password"))
username.send_keys(kb.userName)
password.send_keys(kb.password)
loginBtn = browser.find_element(by=By.CSS_SELECTOR, value=("#loginForm > div > div:nth-child(3) > button > div"))
                               
loginBtn.click()

Any advice ?

like image 987
Selman Avatar asked Dec 13 '25 22:12

Selman


1 Answers

While accessing your Instagram to avoid loging in everytime, once you login for the first time using pickle module you can store the cookies and reuse during your next login attempt as follows:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pickle

driver.execute("get", {'url': 'http://www.instagram.org'})
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("_SelmanFarukYılmaz_")
driver.find_element(By.CSS_SELECTOR, "input[name='password']").send_keys("Selman_Faruk_Yılmaz")
driver.find_element(By.CSS_SELECTOR, "button[type='submit'] div").click()
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
driver.quit()
driver = webdriver.Chrome(service=s, options=options)
driver.execute("get", {'url': 'http://www.instagram.org'})
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
driver.execute("get", {'url': 'http://www.instagram.org'})
like image 87
undetected Selenium Avatar answered Dec 16 '25 16:12

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!