Selenium

Login and navigate a website automatically with Selenium

Selenium is an incredible Python package for automating task in the browser. Essentially, Selenium can be used to script interaction with a website by taking control of the browser using Python. 

This example demonstrates how to complete a login form and navigate to various pages behind the login page using just a few of the many techniques available in the Selenium toolbox. 

This example assumes you already have the relevant driver installed and Selenium install (pip install selenium).

Dependencies

from selenium import webdriver

driver = webdriver.Chrome()

Target the first page 

Give the driver the starting URL and check you've landed where you should by running an assertion on the text in the title of the page:

driver.get("https://ilovefluffycats.com/authentication/signon")

assert "cats" in driver.title

Complete the username and password fields

Find the username field by its id in the HTML markup (e.g. id="uid) and the password by the name attribute (e.g. name="pwd")

username = driver.find_element_by_id("uid")
username.clear()
username.send_keys("mrcats")

password = driver.find_element_by_name("pwd")
password.clear()
password.send_keys("catskillz")

Click the login button

Now we need to submit the login credentials by clicking the submit button

driver.find_element_by_name("submitButton").click()

Click a link on the page based on the link text

This is handy where you know the text of the link you want to target, but there's no unique identifier reliably grip onto in the mark up. Here, we're simply looking for a link with the text: "Grumpy cats".

driver.find_element_by_link_text("Grumpy cats").click()