Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

selenium - 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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...