I've been trying to create a bot that logs into facebook.com for me using the following tutorial: https://medium.com/@kikigulab/how-to-automate-opening-and-login-to-websites-with-python-6aeaf1f6ae98
However, I can't get my bot to click the "Login" button because the ID of Facebook's "Login" button keeps changing (I found this out by right clicking the "Login" button, then inspecting element in Chrome). I suppose they do this to prevent spammers, but how can I log into facebook.com or find a workaround for my problem?
For instance, the ID would be "u_0_d_KK" for one refresh of facebook.com, but change to some other string as soon as a new instance of the website is loaded.
Using the code in the link above, my code would look like this but fail:
def login(url, usernameId, username, passwordId, password, submit_buttonId):
driver.get(url)
driver.find_element_by_id(usernameId).send_keys(username)
driver.find_element_by_id(passwordId).send_keys(password)
driver.find_element_by_id(submit_buttonId).click()
login("https://www.facebook.com/", "email", myFbEmail, "pass", myFbPassword, "u_0_d_KK")
since "u_0_d_KK" would actually be some other string upon loading the website.
Thanks in advance!
CodePudding user response:
See this is the outer HTML
<button value="1" class="_42ft _4jy0 _6lth _4jy6 _4jy1 selected _51sy" name="login" data-testid="royal_login_button" type="submit" id="u_0_d_n6">Login </button>
I see that name is unique in HTMLDOM so, you should use name.
driver.find_element_by_name('login').click()
should work for you. you should write the above line in place of
driver.find_element_by_id(submit_buttonId).click()