How to use Selenium and Selenium WebDriver Manager to Login to a Website with Python?
Last Updated :
24 Apr, 2025
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.
- It also supports parallel test execution which reduces time and increases the efficiency of the test cases.
- 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.
- The tests can be carried out on different OS platforms such as Windows, MAC, and Linux.
- The Selenium test scripts can be integrated with tools such as TestNG and JUnit for managing test cases and generating reports.
- 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.
- This open-source tool provides a flexible programming interface, compatible with multiple programming languages, facilitating the automation of web browsers.
- 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.
- Selenium WebDriver's cross-browser compatibility ensures applications perform consistently across various browsers, a vital consideration in today's diverse web landscape.
- 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 PythonHow 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.
.png)
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.
Similar Reads
In Selenium WebDriver, use Python to obtain the WebElement's HTML source?
The article focuses on discussing how to use Python to obtain the WebElement's HTML source. Prerequisites: Selenium Python Installation: Using Selenium Webdriver, one can obtain the HTML source of any WebElement. What is an HTML Source? HTML Source is described as the HTML code that underlies a cert
2 min read
How can I verify Error Message on a webpage using Selenium Webdriver?
Selenium WebDriver is an essential tool for automating web application testing, allowing testers to ensure that applications function correctly and efficiently. One critical aspect of this testing is verifying error messages, which are crucial for user feedback, especially when invalid inputs are pr
4 min read
How to do session handling in Selenium Webdriver using Java?
In Selenium WebDriver, managing browser sessions is crucial for ensuring that each test runs independently and without interference. A browser session in Selenium is identified by a unique session ID, which helps track and manage the session throughout the test. Proper session handling in Selenium W
4 min read
How to set browser width and height in Selenium WebDriver?
Using Selenium WebDriver, we can set the browser width and height. Various methods are available to set browser width and height. When we run any test, the browser will open in default size. So, if we need to change the size of the browser, we can do it using selenium WebDriver keywords. A few of th
2 min read
Wait Until Page Is Loaded With Selenium WebDriver For Python
Selenium is an automation tool or a web framework used mainly in testing web applications across various browsers. Apart from testing web applications, we can also perform various tasks with selenium. With the help of selenium, we can also perform various web related tasks such as web scraping, web
5 min read
How to select a drop-down menu value using Selenium in Python?
Prerequisite: Browser Automation Using Selenium Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we will be using Python
2 min read
How to switch to new window in Selenium for Python?
Selenium is the most used Python tool for testing and automation for the web. But the problem occurs when it comes to a scenario where users need to switch between windows in chrome. Hence, selenium got that cover as well. Selenium uses these methods for this task- window_handles is used for working
3 min read
How to refresh a webpage using java Selenium Webdriver?
Refreshing a webpage is often necessary when you want to ensure that your tests or interactions are working with the latest content or to reset the state of a web page during automation. Selenium WebDriver makes this task straightforward with various methods available in Java. This article will demo
3 min read
How to Use AutoIT with Selenium Webdriver?
Selenium WebDriver has revolutionized web automation, but it faces limitations when dealing with native Windows dialogs. This is where AutoIT comes to the rescue, bridging the gap between web and desktop automation. For testers and developers working on web applications, understanding how to integra
8 min read
How to make a Selenium jar file, and how to test using a Selenium jar file?
Creating and testing a Selenium jar file is a crucial step in sharing your Selenium automation projects with others or running your tests independently. A JAR file (Java ARchive) packages all your project's classes and resources into a single, executable file. This allows you to easily distribute an
3 min read