Sometimes we may want to automate tasks in a browser, like a login to a website. It can be helpful in scenarios where we want to scrape data from websites that require login.
Python offers many packages for web automation, such as Selenium.
Login to a Website Using Selenium Python
Selenium is a powerful automation and testing tool for web browsers. We write scripts that take control of the web browser and perform specific actions.
Install Selenium and Chrome Webdriver
To install Selenium, we use the following command.
#Python 3.x pip install selenium
ChromeDriver is another executable that Selenium WebDriver uses to interact with Chrome. If we want to automate tasks on the Chrome web browser, we also need to install ChromeDriver.
Based on the version of the Chrome browser, we need to select a compatible driver for it. Following are the steps to install and configure the Chrome driver:
- Click on this link. Download Chrome driver according to the version of your Chrome browser and the type of operating system.
- If you want to find the version of your Chrome browser, click on the three dots on the top right corner of Chrome, click on Help, and select About Google Chrome. You can see the Chrome version in the About section.
- Extract the zip file and run the Chrome driver.
Code for Automated Login to a Website Using Selenium Python
We created the WebDriver instance in the following code and specified the path to the Chrome driver. Then we have specified the
URL of the target website to the driver instance. It will open the target website in the desired browser.
We have stored dummy Email and password in the variables that we will later use to sign in. To automate typing the email and password and clicking the login button, we have to find the
name/id (which one is available) of those web elements.
We have to open the desired website in the browser, right-click on the element, and click Inspect element. Here, we can see the HTML code, including the element’s name.
We will copy the element’s name and pass it as an argument to the
find_element_by_name() method with the driver’s instance. We will repeat the process for the remaining two elements.
This method will return the web element, which we will store in variables. We will automate typing the email and password in their relevant fields’ using the
We set a timer of two seconds after filling the fields so that the user can observe what is happening through a delay in the process. Finally, the login button will automatically click through the
#Python 3.x from selenium import webdriver from selenium.webdriver.common.keys import Keys import time driver = webdriver.Chrome(r"E:\download\chromedriver.exe") driver.maximize_window() driver.get("https://discpersonalitytesting.com/login-here/") email ="email@example.com" password= "mypassword1@23" email_textfield = driver.find_element_by_name("email") password_textfield = driver.find_element_by_name("password") login_button = driver.find_element_by_name("LoginDAPLoginForm") email_textfield.send_keys(email) time.sleep(2) password_textfield.send_keys(password) time.sleep(5) login_button.click()