Open In App

How to use Selenium and Selenium WebDriver Manager to Login to a Website with Python?

Last Updated : 13 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is an open-source tool that is used for automating the tests carried out on different web browsers. Selenium WebDriver is a web framework that allows executing cross-browsing testing. This article focuses on discussing how to log in to a website with Python using Selenium and Selenium WbDriver Manager.

What is Selenium?

Selenium is an open-source tool that is used for automating the tests carried out on web browsers.

  1. It also supports parallel test execution which reduces time and increases the efficiency of the test cases.
  2. Selenium requires fewer resources as compared to other automation testing tools. You don’t need to have different plugins for every case test case on which you are working.
  3. The tests can be carried out on different OS platforms such as Windows, MAC, and Linux.
  4. The Selenium test scripts can be integrated with tools such as TestNG and JUnit for managing test cases and generating reports.
  5. It can also be integrated with Maven, Jenkins, and Docker to achieve continuous testing.

What is Selenium Web Driver?

Selenium WebDriver is a pivotal automation framework for web applications, indispensable to developers and testers alike.

  1. This open-source tool provides a flexible programming interface, compatible with multiple programming languages, facilitating the automation of web browsers.
  2. It empowers users to mimic user interactions with web elements, from clicking buttons and filling forms to navigating complex workflows, making it an ideal choice for web testing and quality assurance.
  3. Selenium WebDriver’s cross-browser compatibility ensures applications perform consistently across various browsers, a vital consideration in today’s diverse web landscape.
  4. Whether you’re automating tests, scraping web data, or streamlining web application interactions, Selenium WebDriver is an invaluable asset in the toolkit of those working with web technologies.

Binding Selenium and Python

Selenium Python bindings provide a simple API to write functional tests using Selenium webdriver. This is where you write your functional tests using Selenium WebDriver. After that, you send a request to the Selenium server. The Selenium server then runs those tests on various browsers, it can Google Chrome, Internet Explorer, Mozilla, Firefox, or Safari.

Binding of Selenium and Python

How to Login Website using Selenium and Selenium Webdriver?

To automate logging into a website using Selenium and Selenium WebDriver manager with Python, you need to follow the following steps:

Step 1: Install Selenium and Selenium Webdriver Manager

To start working with Selenium and Selenium WebDriver Manager, you need to first install them on your computer system. For this, you can use a command line to run the commands.

To install Selenium:

pip install Selenium

To install WebDriver manager:

pip install webdriver-manager

Step 2: Import the necessary modules

Next, on any of your code editors (here, VS Code is used), import the necessary modules required for carrying out the task.

from selenium import webdriverfrom selenium.webdriver.chrome.service import Service as ChromeServicefrom selenium.webdriver.common.keys import Keysfrom webdriver_manager.chrome import ChromeDriverManagerimport time

Make sure you import the appropriate WebDriver manager for your browser.

Step 3: Initialize the webdriver

In this step, create an instance of the WebDriver using the WebDriver Manager. Here, Google Chrome has been used as the browser.

driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

This will automatically download and install the Chrome WebDriver for you.

Step 4: Navigate to the login page

Use the ‘get’ method to navigate and open the login page of the desired website that you want to access.

driver.get(“https://LinkedIn.com/login”)

Replace the URL with the actual login page URL you want to use. In this example, the LinkedIn login page is accessed.

Step 5: Locate and interact with the login elements

Find the HTML elements that represent the username and password fields and the login button. You can do this using various methods like ‘find_element_by_id’, ‘find_element_by_name’, ‘find_element_by_xpath’, or ‘find_element_by_css_selector’. Here,
‘find_element_by_id’ is used.

username_field = driver.find_element_by_id(“username”)password_field = driver.find_element_by_id(“password”)login_button = driver.find_element_by_id(“login-button”)

Step 6: Enter login credentials and submit the form

Use the ‘send_keys’ method to enter your username and password, and then use the ‘click’ method to submit the form.

username_field.send_keys(“your_username”)password_field.send_keys(“your_password”)login_button.click()

Replace “your_username” and “your_password” with your actual login credentials.

Step 7: Close the webdriver

Finally, close the WebDriver when you are done

driver.quit()

Here’s the complete code of the example to help you go through it:

Python




# Python program to login to a website
# using Selenium and Selenium WebDriver
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time
 
try:
    # Initialize the WebDriver
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
 
    # Navigate to the login page
    driver.get("https://LinkedIn.com/login")
    driver.maximize_window()
 
    # Locate and interact with the login elements
    username_field = driver.find_element_by_id("username")
    password_field = driver.find_element_by_id("password")
    login_button = driver.find_element_by_id("login-button")
 
    # Enter login credentials and submit the form
    username_field.send_keys("your_username")
    password_field.send_keys("your_password")
    login_button.click()
 
    # Handle post-login tasks if necessary
 
except Exception as e:
    print(f"An error occurred: {str(e)}")
 
finally:
    # Close the WebDriver
     
    time.sleep(20)
    print(driver.title)
    print(driver.current_url)  
    driver.quit()


Output:

Once, you run the above code in the code editor, it will open the LinkedIn login page in your Chrome browser, where you will be asked to enter your username and password to sign in to your LinkedIn account.

NOTE:

The website on the browser will wait only for 20 seconds as the sleep value is set to 20s.

selenium-op-(1)

Conclusion

In conclusion, there are a plethora of opportunities for automating website logins and many other web interactions when one learns how to use Selenium and the Selenium WebDriver Manager with Python. This potent set of instruments not only makes programmatic website access easier but also offers the flexibility and resilience required to handle the ever-changing online environment. You may use Selenium and the WebDriver Manager to construct dependable, flexible, and effective login automation scripts.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads