How to automate the Facebook login without hard-coding my username and password?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
To automate the Facebook Login without hard-coding the username and password you can use the input()
function to take the user input from the console as follows :
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()
Console Output :
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
回答2:
You should use Facebook OAuth API, never hard-code password, some references:
Facebook SDK for Python
Python Social Auth documentation
Facebook OAuth 2 Tutorial