DEV Community

loading...
Cover image for Get values automatically from browser and set values using selenium

Get values automatically from browser and set values using selenium

Neeraj Gupta
I'm Neeraj and I'm an active part of the Developer Student Clubs of my university. I'm based in India.I'm currently exploring the field of iOS Development.
Updated on ・2 min read

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

  1. FireFox Browser
  2. Time python module (inbuilt module)
  3. Selenium Module
  4. GeckoDriver
  5. Python installed on your machine Python Download.

Install Firefox

Go to above link and download and install Firefox browser.
Alt Text

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()
Enter fullscreen mode Exit fullscreen mode

Stepwise Explanation

  1. Import time module.
  2. Import webdriver from selenium module
  3. Using webdriver we are gonna access Firefox Browser.
  4. Open twitter login page.
  5. Put a pause using time.sleep(5) so browser do not identify you as robot.
  6. Store your twitter mobile/phone/username in userName variable created in code.
  7. 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.
  8. Taking a pause again so as we are not detected as robot.
  9. Store your twitter password in password variable created in code.
  10. 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.
  11. Taking a pause again so as we are not detected as robot.
  12. 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.

Alt Text

  1. Run the script.

Discussion (7)

Collapse
lukeswitz profile image
Luke Switzer

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?

Collapse
neeraj15022001 profile image
Neeraj Gupta Author

I am not that much into automation maybe you can help with the twitter 2FA part.

Collapse
lukeswitz profile image
Luke Switzer

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.

Thread Thread
neeraj15022001 profile image
Neeraj Gupta Author

Okay thanks for the information.

Collapse
neeraj15022001 profile image
Neeraj Gupta Author

It's just a plain script aiming to be implemented as a part of some big automation. Makes sense?

Collapse
andypiper profile image
Andy Piper

What is the use case/purpose here? How does this work with two factor authentication?

Collapse
neeraj15022001 profile image
Neeraj Gupta Author

That case isn't included. This works just fine with accounts not set with two Factor Authentication.