1

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?

Life is complex
  • 15,374
  • 5
  • 29
  • 58

2 Answers2

1

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

Life is complex
  • 15,374
  • 5
  • 29
  • 58
0

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

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • selenium will create a new profile (e.g. Profile 2) and it will use some of the items from the profile (e.g., Profile 1) being passed. Each time I try to use a stored password it does not pass to the site linked that account info. – Life is complex Feb 18 '21 at 20:49
  • @Lifeiscomplex if you pass a profile then selenium will not create a profile . It will be created only if nothing is passed or given profile doesn't exists – PDHide Feb 18 '21 at 20:54
  • in the above mentioned case it will not create profile but use your default profile which has the saved session – PDHide Feb 18 '21 at 20:55
  • it won't use anything from profile 1 in profile 2 , it will be a new profile with user-data folder same . But sessions might not work as there will session details inside profile folder also . so you have to use both userdata and profile – PDHide Feb 18 '21 at 20:58
  • My code passes a profile that has saved login info, but that info isn't passed to the service. When I look at the profile it is new with no saved login details. When I check the other profiles there is NO saved login info. – Life is complex Feb 18 '21 at 21:15
  • please add the code you used – PDHide Feb 18 '21 at 21:16
  • https://stackoverflow.com/a/65140078/6793637 – PDHide Feb 18 '21 at 21:17
  • My code is in my answer. – Life is complex Feb 18 '21 at 21:17
  • there might not be any profile 2 thats why its creating a new one , if you login to facebook in that stage then for second automation run it should not ask for password – PDHide Feb 18 '21 at 21:19
  • I removed the OPs Windows variables from my answer. The login info is not being passed and it is saved in the Chrome outside of selenium. In selenium that information does not exist when being passed. – Life is complex Feb 18 '21 at 21:26
  • I looked through https://groups.google.com/g/selenium-users and https://github.com/SeleniumHQ/selenium/issues. I don't think it is possible to auto login by passing a profile to chromedriver. I voted to reopen this question, because that is what the OP asked to do. – Life is complex Feb 18 '21 at 22:48