Selenium 101: How To Automate Your Login Process
  January 31, 2018

While learning Selenium can surely be challenging in the shift from manual to automation, starting small and making the effort to be continuously learning will help you become proficient in no time.

CrossBrowserTesting wants to help your team get started with automated testing, which is why we’re creating Selenium 101 guides to teach you the basics. By the end, every software team will want you scripting tests for them.

For the first of the series, we’re starting at the very beginning by showing you how to automate a simple login process with Selenium using Python (because it’s one of the easiest programming languages to learn first).

You’ll want to install Python, Chrome Driver, and Selenium before starting. ChromeDriver will come in the form of an executable (Windows) or a binary (Mac/Unix). Those technical details aren’t too important now, but you’ll need the file itself. You can get the latest release of ChromeDriver here. Use the following command to add the Selenium library to Python.

pip install selenium

You’ll also need to import some specific modules from Python’s Selenium library. At the bare minimum, you’ll need to do the following:

from selenium import webdriver

Finally, we need to actually start a webdriver. We can do so with only one line of code:

# if chromedriver is not in your path, you’ll need to add it here

driver = webdriver.Chrome()

Now, we can get started automating a simple task like your website’s login form. Basically what we want to do is navigate to the website, locate the username and password fields, enter your credentials, and submit them to get past your login screen. You might also want to define a “method” (something you can call repeatedly) so you can reuse it within other tests. It’ll look something like this:

def site_login():

driver.get (“URL”)

driver.find_element_by_id(“ID”).send_keys(“username”)

driver.find_element_by_id (“ID”).send_keys(“password”)

driver.find_element_by_id(“submit”).click()

For example, if we were to be automating a Facebook login, it would look something like this:

def fb_login():

driver.get (“https://www.facebook.com”)

driver.find_element_by_id(“email”).send_keys(‘fakeemail@crossbrowsertesting.com’)

driver.find_element_by_id(“pass”).send_keys(“fakepassword1”)

driver.find_element_by_id(“loginbutton”).click()

There’s more than one way to locate the elements of your web application in order to find the username and password fields, and some may not always be available to you depending on the way your webpage was written. You can find elements by ID, as we exemplified, but you can also locate them by name, XPath, CSS Selectors, and more. To read about the different ways to locate elements during your login process, read Selenium’s official documentation.

Some pages use dynamic content (meaning lots of JavaScript!). To handle this effectively, we sometimes need to wait for some event to occur. To check and make sure that the login was successful, you might want locate an element on the page you’d land on after your login form by using a wait. We’ll need a few more components from the Selenium library. With the same example, you could do it by locating an element by like this:

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(driver, 10).until(EC.title_contains("home"))

The above code will wait a maximum of 10 seconds while attempting to find the “Home” button displayed when you first login to Facebook. Again, there are a few different ways to go about this including waiting for an element to be clickable, visible, or present on the page. You can read more about that here.

There you have it; this should provide a basic foundation for automating a simple login process with Selenium in Python. Take a look at the rest of our Selenium 101 series to continue on your journey to becoming a test automation master: