Selenium

Creating user interaction based web scripts

Created: July 7th 2019
Updated: December 7th 2019
Connect with us on
image
image

Selenium, and the webdriver API are used for creating user interaction based web scripts. This can be everything from automation to end-user testing, to scraping.

Definitions

Some terms to help you navigate the post

Selenium

A module that allows you to instantiate browser windows and control them using python.

Webdriver

An object within the module that allows you to instantiate and interact with a browser.

Usage

Requirements

For this demo you will need a few things, i've outlined the requirements below and how to set them up:

  1. Selenium: to get selenium you can either use pip install selenium or in the folder with this readme run pip install -r requirements.txt
  2. A webdriver: This demo is using the selenium webdriver API which means you need a webdriver installed and added to PATH, details for the chrome webdriver used in this demo can be found here. You can also take a look at this package to manage this automatically.

Running

After you have obtained the requirements. in this repo you can see the demo code and actually run it by running python login_to_youtube.py or python3 login_to_youtube.py

Real World Applications

In this repo you can see a real world application, the login_to_youtube.py file does exactly what it implies, it allows you to login to a chrome window using python.

Another great example would be if you are using a CMS like Drupal or WordPress and you want to take a bunch of lines of data and input it to a website that you don't have database access to you could write a selenium scrip that:

  1. Parses and organizes the data
  2. Logs in and inputs the data in the exact same way you would, using the front end fields

Files

login_to_youtube_demo.py

"""Description:
A demo of the selenium module, specifically the WebDriver API
Variables:
browser (webdriver):The main browser instance used to do everything with
"""
import time # Used to add delay between pages loading
import getpass # Allows you to type password without people seeing it

from selenium import webdriver # What allows you to instantiate a chrome browser
from selenium.webdriver.common.keys import Keys # Allows you to send keys like enter and ctrl etc.
from selenium.common.exceptions import WebDriverException # Gets thrown if chrome webdriver is not set up properly

try: # Try to create a chrome browser; Note other browsers work, I just had chrome setup see here for full ist https://selenium-python.readthedocs.io/api.html
    browser = webdriver.Chrome() # Global variable often makes sense because even API calls need an instantiated browser
except WebDriverException: # Gets thrown if chrome webdriver is not set up properly
    print("Chrome Webdriver not found in path please see: https://sites.google.com/a/chromium.org/chromedriver/home for installation instructions")
    exit()

def login_to_youtube(email, password):
    """A function to automate logging into youtube.com
    Arguments:
    email(str): The account you would like to login with's email
    password(str): The account you would like to login with's password
    """
    browser.get("https://www.youtube.com/")
    browser.find_element_by_xpath("//*[@aria-label='Sign in']").click() # Find sign in button and click it
    time.sleep(3) # Adding delay while page loads; see https://selenium-python.readthedocs.io/waits.html for better way to do this
    browser.find_element_by_xpath("//*[@aria-label='Email or phone']").send_keys(email + Keys.ENTER) # type provided email into box
    time.sleep(2) # Adding delay while page loads; see https://selenium-python.readthedocs.io/waits.html for better way to do this
    browser.find_element_by_xpath("//*[@aria-label='Enter your password']").send_keys(password + Keys.ENTER) # type provided password in

if __name__ == "__main__":
    email = input("What's your email?: ")
    password = getpass.getpass("Enter your password: ")
    login_to_youtube(email, password)

Thank you for your support!

Be sure to share the post if it was helpful!