How to pass profile with stored login information to selenium and force an auto login process

Viewed 1624

I'm trying to import my Chrome profile for use in chromedriver. I want to use the stored login information in that profile to automatically log in to Facebook using selenium.

My current code:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument('C:\\Users\\Jose Vitor\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2')
driver = webdriver.Chrome(executable_path="C:/Program Files (x86)/chromedriver.exe", options=options)
driver.get('https://www.facebook.com/')

This code opens Chrome using chromedriver, but it creates 3 blank tabs and Facebook is not logged into automatically

Could someone help me solve the automatic login process to Facebook using chromedriver with a stored profile?

2 Answers

UPDATE


I could not get the stored login for Facebook to pass to a profile in selenium. So I put together some login code for you.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

# global driver
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

driver.get('https://www.facebook.com')
driver.implicitly_wait(10)
username_box = driver.find_element_by_xpath('//*[@id ="email"]')
username_box.send_keys('username')

password_box = driver.find_element_by_xpath('//*[@id ="pass"]')
password_box.send_keys('password')

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.NAME, "login"))).click()

ORIGINAL POST


Here is some code that only opens 1 tab and pass your profile.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")

# you need to verify this path on your Windows systems.
chrome_options.add_argument("user-data-dir=/Users/username/Library/Application Support/Google Chrome/User Data")

chrome_options.add_argument("profile-directory=Profile 1")

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

driver.get('https://www.facebook.com')

Have you tried to write any code around logging into Facebook after Chrome opens? if not, looks at some of these past questions: https://stackoverflow.com/search?q=login+into+facebook+with+selenium+python

open chrome that works without needing to login to facebook and run chrome://version/ and then copy the highlighted part:

enter image description here

Close all chrome instance and use: (Dont give last directory )

options.add_argument(--user-data-dir='C:\\Users\\Jose Vitor\\AppData\\Local\\Google\\Chrome\\User Data');

This will use your default chrome profile it will have your login information . You may not have created profile correctly don't give profile-directory when using default as it will be taken automatically

Related