如何在python 2.7中使用自动登录脚本中浏览器保存的凭据?

ste*_*o91 7 python selenium webdriver

当我手动打开浏览器,firefox和chrome,然后进入我之前通过浏览器保存登录凭据的网站时,用户名和密码字段会自动填充.但是,当我使用python selenium webdriver打开浏览器到特定页面时,字段不会填充.

我的脚本的目的是打开网页并使用element.submit()登录,因为登录凭据应该已经填充.但是不是.我怎样才能让他们进入田野?

例如:

driver = webdriver.Chrome()    
driver.get("https://facebook.com")    
element = driver.find_element_by_id("u_0_v")    
element.submit()
Run Code Online (Sandbox Code Playgroud)

bmc*_*ley 13

这是因为selenium不使用您的默认浏览器实例,它会使用临时(空)配置文件打开另一个实例.

如果您希望加载默认配置文件,则需要指示它执行此操作.

这是一个chrome示例:

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)
Run Code Online (Sandbox Code Playgroud)

这是一个firefox示例:

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

profile = FirefoxProfile("C:\\Path\\to\\profile")
driver = webdriver.Firefox(profile)
Run Code Online (Sandbox Code Playgroud)

我们在这里,只是在(非官方)文档中挖出了这个链接.Firefox配置文件和Chrome驱动程序信息就在它下面.

  • 您是英雄伴侣,像魅力一样工作。顺便说一句,我花了一些时间来找出Chrome配置文件的正确路径,对于任何认为它应该是的人:C:\ Users \ xxxxx \ AppData \ Local \ Google \ Chrome \ User Data (3认同)
  • 在 Ubuntu 上,您的个人资料将类似于:`profile = FirefoxProfile("/home/username/.mozilla/firefox/xxxxxxx.default")` (3认同)
  • 对于那些对 Firefox 的 Window 路径感兴趣的人 C:\Users\xxxxx\AppData\Local\Mozilla\Firefox\Profiles (2认同)