【问题标题】:How to provide credentials as user input runtime during python automation?如何在 python 自动化期间提供凭据作为用户输入运行时?
【发布时间】:2018-08-22 20:36:07
【问题描述】:

如何在不硬编码我的用户名和密码的情况下自动登录 Facebook?

【问题讨论】:

  • 如果不对细节进行硬编码,这将不是一个自动化过程。您可以在调用send_keys 之前使用input("Enter username"),密码也是如此。但不确定这将如何自动化该过程。
  • 啊,没关系,如果需要在运行时获取该用户名和密码作为输入。
  • 在自动化时硬编码我们的凭证是否好?或者当我们在运行时提供信息时它会是安全的?。

标签: python python-3.x selenium selenium-webdriver user-input


【解决方案1】:

你应该使用 Facebook OAuth API,永远不要硬编码密码,一些参考:

Facebook SDK for Python

Python Social Auth documentation

Facebook OAuth 2 Tutorial

【讨论】:

  • 赞成有效的参考。
【解决方案2】:

要自动化 Facebook 登录 而无需对 用户名密码 进行硬编码,您可以使用 input() 函数获取 用户输入来自控制台如下:

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://www.facebook.com/")
emailid = input("What is your emailid?(Press enter at the end to continue):")
driver.find_element_by_xpath("//input[@id='email']").send_keys(emailid)
password = input("What is your password?(Press enter at the end to continue):")
driver.find_element_by_xpath("//input[@id='pass']").send_keys("password")
driver.find_element_by_xpath("//input[starts-with(@id, 'u_0_')][@value='Log In']").click()

控制台输出:

What is your emailid?(Press enter at the end to continue):gomathisubramanian@gmail.com
What is your password?(Press enter at the end to continue):gomathisubramanian

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多