问题描述
当我手动打开浏览器(firefox 和 chrome)并继续访问我之前通过浏览器保存登录凭据的网站时,用户名和密码字段会自动填充.但是,当我使用 python selenium webdriver 打开浏览器到特定页面时,不会填充这些字段.
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.
我的脚本的重点是打开网页并使用 element.submit()
登录,因为应该已经填充了登录凭据.但不是.我怎样才能让它们在田野中填充?
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?
例如:
driver = webdriver.Chrome()
driver.get("https://facebook.com")
element = driver.find_element_by_id("u_0_v")
element.submit()
推荐答案
这是因为 selenium 不使用您的默认浏览器实例,它会打开一个具有临时(空)配置文件的不同实例.
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.
这是一个 chrome 示例:
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)
这是一个火狐的例子:
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
profile = FirefoxProfile("C:\Path\to\profile")
driver = webdriver.Firefox(profile)
我们开始吧,只需在(非官方)文档中找到此链接.Firefox 配置文件 和 Chrome驱动程序信息就在它的下方.
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.
这篇关于如何在 python 2.7 的自动登录脚本中使用浏览器保存的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!