'How do you use credentials saved by the browser in auto login script in python 2.7?

When I manually open a browser, both firefox and chrome, and proceed to a website where I previously saved my login credentials via the browser, the username and password fields automatically populate as they should. However, when I use the python selenium webdriver to open the browser to a specific page, the fields do not populate.

The point of my script is to open the webpage and use element.submit() to login since the login credentials should already be populated., but the are NOT. How can i get them to populate in the fields?

For Example:

driver = webdriver.Chrome()    
driver.get("https://facebook.com")    
element = driver.find_element_by_id("u_0_v")    
element.submit()


Solution 1:[1]

This is because selenium doesn't use your default browser instance, it opens a different instance with a temporary (empty) profile.

If you would like it to load a default profile you need to instruct it to do so.

Here's a chrome example:

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

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

And here's a firefox example:

from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile

profile = FirefoxProfile("C:\\Path\\to\\profile")
driver = webdriver.Firefox(profile)

Here we go, just dug up a link to this in the (unofficial) documentation. Firefox Profile and the Chrome driver info is right underneath it.

Solution 2:[2]

from selenium import webdriver
from os.path import abspath
from os import path
from time import sleep

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\...")  # Path to your chrome profile or you can open chrome and type: "chrome://version/" on URL

chrome_driver_exe_path = abspath("./chromedriver_win32/chromedriver.exe")  # download from https://chromedriver.chromium.org/downloads
assert path.exists(chrome_driver_exe_path), 'chromedriver.exe not found!'
web = webdriver.Chrome(executable_path=chrome_driver_exe_path, options=options)

web.get("https://www.google.com")
web.set_window_position(0, 0)
web.set_window_size(700, 700)
sleep(2)
web.close()

Solution 3:[3]

If it navigates to "data/default" you can put your new url in that tab by doing driver.switch_to_window(driver.window_handles[0]).

Solution 4:[4]

options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Profile num\\")
path = os.path.abspath(filedialog.askopenfile().name)
driver1 = webdriver.Chrome(executable_path=path,options=options)

before you do this open chrome and add a profile then go into the profile and paste the contents of the profile into the default dir

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bmcculley
Solution 2 Carson
Solution 3 Jeffrey Kozik
Solution 4 mezza