Create an easy auto-login robot using Python and Selenium

0 4

Programmers are known for their ability to automate. Automating repetitive tasks, boring or time-consuming without the need for a script is a common use of automation.
Web automation allows you to easily build a bot that can perform various tasks online, such as monitoring hotel prices across the Internet to determine the most competitive price.
Personally, I have always found logging into my email fairly repetitive and boring, so for the sake of a simple example to get you guys started with web automation, let’s implement an automated Python script to log in with a single click to a Gmail account.

Setup and Installation

We will be using the following tools in this tutorial:

  • Programming language in Python
  • Google Chrome browser
  • Automation toolkit for Selenium browsers
  • Chrome Driver web driver for Chrome
  • We will use Python 2.7.11 for our program. Installing a newer version of Python 2 is crucial, as it includes PIP. This will enable us to install third party packages and frameworks to help automate our scripts.
    For the changes to be effective, reboot your computer after they are installed. To add Selenium to Python, use the command pip to install it. Selenium allows us to scroll and copy text as well as fill out forms.

    The executable Selenium Chrome Driver is now available. This will allow you to open Google Chrome when necessary for your automated tasks. Chrome Driver simply opens Google Chrome to programmatically access the standard browser operations.
    Download the latest ZIP file here. Extract the chromedriver.exe executable and then place it in your desired directory. We will require your executable once we start.

    The Program Starts

    As aforementioned, we’ll be using the Selenium web automation framework in order to log in programmatically. The first order of business is to import every module we’ll be needing from the Selenium Python library which we installed ealier with PIP.
    Let’s open IDLE or another code editor, create a new Python file with .py extension, and import the following modules:

    Selenium import websitedriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    Next, we’ll create two strings that represent our username and password for the email account. Attention to capitalization in password strings.

    usernameStr=”putYourUsernameHere”
    passwordStr=”putYourPasswordHere”

    Now that we have everything set up in order to start the login process, we need to actually instruct a browser window to open Google Chrome, and navigate to Gmail’s login page.
    If you haven’t done so already, make sure your Python script is saved in the same location as the chromedriver.exe executable we extracted earlier.

    chrome = webdriver.Chrome()
    browser.get((‘https://accounts.google.com/ServiceLogin?’
    ‘service=mail&continue=https://mail.google’
    ‘.com/mail/#identifier’))

    Finding Our Element

    We’ve successfully gotten the browser to open Gmail, but now we need to find the input fields on the web page, into which we can enter our username and password.
    This is possible with the Chrome Developer Tools. We only need to right-click on the input fields, and select the “Inspect” menu.

    Now it’s just a matter of using HTML id attributes to find the elements programmatically.
    In the code below, we’re simply searching for the username input field by id, and filling it with the desired text. Then we’re searching for the Next button which transitions with a quick animation before prompting us for the password.
    Send_keys()You can click() commands do exactly as their names suggest — send_keys()Click to simulate keypresses on the desired element.()Simulates mouse click

    # Fill in your username, and click the Next button
    username = browser.find_element_by_id(‘Email’)
    username.send_keys(usernameStr)
    nextButton = browser.find_element_by_id(‘next’)
    nextButton.click()

    We can do the same thing for the password input field, as well as for the Sign in button. These two items will not appear on your page unless you make an animated transition.
    The program must wait for a while before it resumes its search for elements. This stage is essentially the same as the one before. Only we need to instruct the browser not to locate the password entry for more than 10 seconds.

    # Wait for the transition, then fill in items
    password = WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.ID, ‘Passwd’)))
    password.send_keys(passwordStr)

    signInButton = browser.find_element_by_id(‘signIn’)
    signInButton.click()

    The Final Words

    You’ve just finished creating a bot that visits a web page, enters your username and password, and successfully sign you in, automating the whole process to a single click. Although this is a very simple example, there are many other possibilities.
    This means that you need to be cautious about how this skill is used. Many people abuse automated scripts and bots to win sweepstakes hundreds of thousands of times. They ignore terms and conditions. Some people use them to commit more serious crimes.
    Automating is essential in programming, so make sure you use it carefully and with purpose.

    Leave A Reply

    Your email address will not be published.