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 ?
CodePudding user response:
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("_devanjan_")
driver.find_element(By.CSS_SELECTOR, "input[name='password']").send_keys("dev__anjan")
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'})