Overview
Taking example of twitter account to automatically login without opening browser or wanting this feature for some other automation you are working on. Here is the script i wrote in which you have to add your account details once and then whenever you want to directly login just run the script rest of the work will be done from opening browser to logging in to twitter.
Prerequisites
- FireFox Browser
- Time python module (inbuilt module)
- Selenium Module
- GeckoDriver
- Python installed on your machine Python Download.
Install Firefox
Go to above link and download and install Firefox browser.
Install Selenium Module
pip install selenium
Install Geckodriver
pip install geckodriver-autoinstaller
Script
import time
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("https://twitter.com/login")
time.sleep(5)
userName = "Your Phone or Mail or userName"
browser.find_element_by_name("session[username_or_email]").send_keys(userName)
time.sleep(5)
password = "Your Password"
browser.find_element_by_name("session[password]").send_keys(password)
time.sleep(5)
browser.find_element_by_css_selector("#react-root > div > div > div.css-1dbjc4n.r-13qz1uu.r-417010 > main > div > div > div.css-1dbjc4n.r-13qz1uu > form > div > div:nth-child(8) > div > div").click()
Stepwise Explanation
- Import time module.
- Import webdriver from selenium module
- Using webdriver we are gonna access Firefox Browser.
- Open twitter login page.
- Put a pause using time.sleep(5) so browser do not identify you as robot.
- Store your twitter mobile/phone/username in userName variable created in code.
- Filling in username field on twitter login page. Using find_element_by_name, we are getting the field used for getting username and using send_keys, we are inputting value in it.
- Taking a pause again so as we are not detected as robot.
- Store your twitter password in password variable created in code.
- Filling in password field on twitter login page. Using find_element_by_name, we are getting the field used for getting password and using send_keys, we are inputting value in it.
- Taking a pause again so as we are not detected as robot.
- Using find_element_by_css_selector we are getting the button present on login page. We can get css selector path by going to login page of twitter and right clicking on login button and inspecting it and then in inspector navigate to element highlighted by inspector and then right click on it and press copy path and select copy selector.
- Run the script.
Discussion (7)
Plain text pw store? No 2FA? What madness is this script hoping to accomplish other than leaking creds & revealing you don’t use two factor?
I am not that much into automation maybe you can help with the twitter 2FA part.
Automating two factor eliminates its efficacy. Scripting as above reserved for burner accounts. Likely to get you flagged as Twitter is on very high alert for the foreseeable future.
Okay thanks for the information.
It's just a plain script aiming to be implemented as a part of some big automation. Makes sense?
What is the use case/purpose here? How does this work with two factor authentication?
That case isn't included. This works just fine with accounts not set with two Factor Authentication.