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:
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:
driver.get (“https://www.facebook.com”) driver.find_element_by_id(“email”).send_keys(‘firstname.lastname@example.org’) 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.
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:
- Selenium 101: Running Your Test Against Two Browsers at Once
- Selenium 101: Navigating Waits and Sleeps
- Selenium 101: How to Take Screenshots During Automated Tests
- Selenium 101: Automating User Actions
- Selenium 101: Generating Reports
- Selenium 101: Mastering Selenium Design Patterns
- Selenium 101: Managing Multiple Tabs