Creating user interaction based web scripts

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

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.


Some terms to help you navigate the post


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


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



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.


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

Real World Applications

In this repo you can see a real world application, the 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


A demo of the selenium module, specifically the WebDriver API
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
    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: for installation instructions")

def login_to_youtube(email, password):
    """A function to automate logging into
    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.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 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 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!