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

新手上路,请多包涵

当我手动打开浏览器(包括 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()

原文由 DevSteve 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 652
2 个回答

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

如果您希望它加载默认配置文件,您需要指示它这样做。

这是一个镀铬示例:

 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)

这是一个 Firefox 示例:

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

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

我们开始吧,只是在(非官方)文档中找到了一个链接。 Firefox 配置文件 和 Chrome 驱动程序信息就在其下方。

原文由 bmcculley 发布,翻译遵循 CC BY-SA 3.0 许可协议

  • 下载 chromedriver.exehttps ://chromedriver.chromium.org/downloads
  • 显示 chrome 版本和配置文件:打开 chrome 并在 URL 上输入:“ chrome://version/ ” “` 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()

”`

原文由 Carson 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题