Search code examples
pythonseleniumgoogle-chromejupyter-notebookinstagram

Is there a way to screenshot instagram without making it logging in more than once in selenium?


Is there a way where every time selenium tries to screenshot an instagram account, it only needs to login once?

import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
from time import sleep
from credentials import userName,password

exist = ["duolingo", 'duolingobrasil', 'duolingodeutschland', 'duolingofrance', 'duolingespanol']

DRIVER = 'chromedriver'
driver = webdriver.Chrome(DRIVER)

i = 1
for ss in exist:
    chrome_options = Options() #
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("--window-size=1920x1080")
    DRIVER = 'chromedriver'
    driver = webdriver.Chrome(DRIVER)
    browser = webdriver.Chrome(executable_path=DRIVER)
    browser.implicitly_wait(5)
    browser.get('https://www.instagram.com/accounts/login/')
    sleep(10)
    username_input = browser.find_element(By.CSS_SELECTOR,"input[name='username']")
    password_input = browser.find_element(By.CSS_SELECTOR,"input[name='password']")
    username_input.send_keys("userName")
    password_input.send_keys("password")
    login_button = browser.find_element(By.XPATH, "//button[@type='submit']")
    login_button.click()
    browser.close()
    driver.get("https://www.instagram.com/"+ss+"/")
    time.sleep(10)
    screenshot = driver.save_screenshot('Pictures/duolingo' + str(i) + '.png')
    i += 1
driver.quit()

every time I tried to screenshot the output always comes out like this after going into the account's profile.

Instagram login


Solution

  • You can achieve this using profile - Chrome profile.

    I couldn't able to verify this code, because my instagram account has some problems, but this code should work.

    There are some flaws in your code, I've modified it.

    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    # path of the chrome's profile parent directory, you can mention your own
    chrome_options.add_argument("user-data-dir=C:\\Temp")  
    # name of the directory
    chrome_options.add_argument("--profile-directory=ChromeData")
    
    # chrome_options.add_argument('--headless')
    url = "https://www.instagram.com/"
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = chrome_options)
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    
    username_input = driver.find_element(By.CSS_SELECTOR, "input[name='username']")
    password_input = driver.find_element(By.CSS_SELECTOR, "input[name='password']")
    username_input.send_keys("<username>")
    password_input.send_keys("<password>")
    login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
    login_button.click()
    sleep(5)
    driver.quit()
    
    exist = ["duolingo", 'duolingobrasil', 'duolingodeutschland', 'duolingofrance', 'duolingespanol']
    
    i = 1
    for ss in exist:
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
        driver.maximize_window()
        driver.implicitly_wait(10)
        driver.get(f"https://www.instagram.com/{ss}/")
        sleep(2)
        screenshot = driver.save_screenshot('./screenshots/duolingo' + str(i) + '.png')
        sleep(2)
        driver.quit()
        sleep(5)
        i += 1
    

    Try the above code, if you are facing any issue, add a comment.