Login to any website with Selenium

Learn how to interact with webpages, fill up forms and login to any platform

Igor Zabukovec
3 min readJan 17, 2019

What you will learn in this tutorial

For thus tutorial you’ll need to have basic knowledge about Selenium — and have Selenium up and running. If you don’t, I suggest you read the first part of the Tutorial Series.

Sometimes, some websites require login to access information and automate some tasks. For instance, it is the case if the site does not have an api, or is poorly supported, or too restrictive.

In this tutorial we’ll use the Twitter example, but I’ll post some more examples on Github regularly.

Load the home page

If you followed our first tutorial, you know how to create a Chrome Instance and load a url.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=your_exec_path)url = "https://twitter.com/"driver.get(url)
time.sleep(3)

You should then be seeing this page:

Twitter Home page

Note that we have decided to be in “Incognito mode” and we decided to wait 3 seconds just in case the page takes a long time to load.

Select the form elements

In this login form we are interested in the username, password and login button elements. Using Selector Gadget, we get that the css_selector are:

username: .js-signin-email

password: .LoginForm-password .text-input

submit: .js-submit

Now that we have the CSS selectors, we can get the corresponding elements:

elements = driver.find_elements_by_css_selector(".js-signin-email")
username = elements[0]
elements = driver.find_elements_by_css_selector(".LoginForm-password .text-input")
password = elements[0]
elements = driver.find_elements_by_css_selector(".js-submit")
submit = elements[0]

Fill up the form

We will start with the username. The password will follow.

Clear the field

First thing to do is to clear the input field to get rid of the placeholder.

Even if there is not placeholder, it is always recommended to clear the field.

The command to clear a field is simply: element.clear(). In our case:

elements = driver.find_elements_by_css_selector(".js-signin-email")
username = elements[0]
username.clear()

Type your input

Once you have cleared the input, you can now ask Selenium to type your credentials — which should be valid. In our example we will use “foo” and “bar”.

The command to type in an input field is : element.send_keys(“input”). In our case:

user_name = "foo"
username.send_keys(user_name)

If you run the above commands, you should see this screen where your username is typed in.

Same logic for your password

For your password, we will use the exact same logic. Our generic password will be “bar” and you should use your’s if you want to login :)

elements = driver.find_elements_by_css_selector(".LoginForm-password .text-input")
password = elements[0]`
password.clear()
password.send_keys("bar")

Quite simple isn’t it?

Submit your form

To submit the form we just have to tell Selenium to click on the submit button.

The command to type in an input field is : element.click(). In our case:

elements = driver.find_elements_by_css_selector(".js-submit")
submit = elements[0]
submit.click()

Wrap up your second Selenium Script

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=your_exec_path)url = "https://twitter.com/"
user = "foo"
pass = "bar"
driver.get(url)
time.sleep(3)
elements = driver.find_elements_by_css_selector(".js-signin-email")
username = elements[0]
username.clear()
username.send_keys(user)
elements = driver.find_elements_by_css_selector(".LoginForm-password .text-input")
password = elements[0]`
password.clear()
password.send_keys(pass)
elements = driver.find_elements_by_css_selector(".js-submit")
submit = elements[0]
submit.click()

Et voila ! You’ve done it ! I recommend using this logic it on different platforms and websites.

--

--

Igor Zabukovec

CTO / Data Scientist — Specialising in scalable intelligent systems and high-value products for business and consumer.