3

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 ?

1 Answer 1

5

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'})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.