Python Selenium Webdriver Example Tutorial For Beginners

Python's Selenium WebDriver is a powerful tool for automating browser activities, including testing web applications. It's widely used for cross-browser testing, scraping websites, and filling out forms. Let's dive into using Selenium with Python, exploring setup, a step-by-step example, and common use cases.

Set up Selenium &  GeckoDriver (Mac)
Set up Selenium & GeckoDriver (Mac)

Before we begin, ensure you have Python installed, along with the WebDriver for your browser of choice. You can download and install WebDriver from the official Selenium site or use a package like webdriver_manager to handle automated downloading.

Selenium 4 Python Tutorial for Beginners
Selenium 4 Python Tutorial for Beginners

Setting Up Selenium WebDriver with Python

First, install the Selenium package using pip:

Selenium Python Tutorial with WebDriver (Example)
Selenium Python Tutorial with WebDriver (Example)

pip install selenium

Importing Selenium and Setting Up the WebDriver

How to Install Selenium WebDriver on Any Computer With Python
How to Install Selenium WebDriver on Any Computer With Python

Start by importing Selenium and initializing your WebDriver.

from selenium import webdriver driver = webdriver.Firefox() # or Chrome, Safari, etc.

Launching and Navigating a WebPage

How to Perform Mouse Hover in Selenium with Python
How to Perform Mouse Hover in Selenium with Python

Next, use the driver to launch your browser and navigate to a webpage.

driver.get("https://www.example.com")

Selenium WebDriver Example: Automating a Login Form

Modern Web Automation With Python and Selenium – Real Python
Modern Web Automation With Python and Selenium – Real Python

Now let's automate a simple login process on a website, step by step. For this example, we'll use the 'Have I Been Pwned' site (https://haveibeenpwnd.com/), which has a simple login form.

Before proceeding, ensure that you've agreed to the site's terms of service and that you're using a test email for logging in.

Python Selenium Locate Elements
Python Selenium Locate Elements
How to Install Selenium WebDriver on Any Computer With Python
How to Install Selenium WebDriver on Any Computer With Python
Download Free Cheat Sheets or Create Your Own! - Cheatography.com: Cheat Sheets For Every Occasion
Download Free Cheat Sheets or Create Your Own! - Cheatography.com: Cheat Sheets For Every Occasion
Python tkinter Toolbox
Python tkinter Toolbox
Selenium With Python Tutorial for Beginners | TestGrid Blog
Selenium With Python Tutorial for Beginners | TestGrid Blog
Selenium WebDriver Commands (Methods) - Scientech Easy
Selenium WebDriver Commands (Methods) - Scientech Easy
Automate 99% of Websites with Selenium 4 and Python
Automate 99% of Websites with Selenium 4 and Python
Selenium IDE Tutorials for Beginners
Selenium IDE Tutorials for Beginners
How I saved 8 Hours, Automating my Gov Appointment Hunt Using Python + Selenium
How I saved 8 Hours, Automating my Gov Appointment Hunt Using Python + Selenium
Python Selenium Tutorial #3 - Web Scraping to Excel
Python Selenium Tutorial #3 - Web Scraping to Excel
Python Automation Ideas That Actually Save Time (With Tools)
Python Automation Ideas That Actually Save Time (With Tools)
Easy webscraper bot with Python!
Easy webscraper bot with Python!
an image of a computer screen with the text gauge chart using python
an image of a computer screen with the text gauge chart using python
🐍 Python Web Scraper: Automatizá tu Sourcing | WebSquad DigitalCon
🐍 Python Web Scraper: Automatizá tu Sourcing | WebSquad DigitalCon
Ezekiel (@ezekiel_aleke) on X
Ezekiel (@ezekiel_aleke) on X
Twitter scraper tutorial with Python: Requests, BeautifulSoup, and Selenium — Part 2
Twitter scraper tutorial with Python: Requests, BeautifulSoup, and Selenium — Part 2
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
python cheat sheet. coding python #interview #job #hiring
python cheat sheet. coding python #interview #job #hiring
How to Automate an Excel Sheet in Python?
How to Automate an Excel Sheet in Python?
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Locating Elements with Selenium

Selenium's driver methods like find_element_by_* are used to locate elements on the page. Here, we'll find the username and password fields.

user_name = driver.find_element_by_name("username") password = driver.find_element_by_name("password")

Filling Out and Submitting the Form

Now, send your credentials to the respective fields and submit the form.

user_name.send_keys("test@example.com") password.send_keys("testpassword") driver.find_element_by_xpath('//input[@type="submit"]').click()

Handling Alerts

After submitting the form, an alert pops up. Selenium's switch_to_alert method can help you handle this:

alert = driver.switch_to.alert alert.accept()

Verifying the Result

Finally, check if the login was successful by verifying the page title:

assert "Account" in driver.title

Common Use Cases for Selenium WebDriver

Selenium WebDriver is used extensively in test automation, browser compatibility testing, and web scraping. Its vast capabilities make it a crucial tool in modern web development and testing toolkits.

To explore more features, consider learning about WebDriver's API, handling frames, working with cookies, and using WebDriverWait for conditional commands.

Easy Cross-Browser Testing

Selenium allows you to run tests on various browsers (Chrome, Firefox, Safari, Edge) and operating systems. This ensures your web application's consistency across different user environments.

Data-Driven Testing

With Selenium, you can load test data from CSV or Excel files, enabling data-driven test and automation. This makes it easier to test your application with diverse data sets.

As you've seen, Selenium WebDriver is a versatile tool that can greatly simplify your web testing and automation tasks. To learn more, consider exploring its rich documentation and examples or joining its active online community.

The possibilities are endless, so start automating and Happy Testing!

Related Articles

Nested While Loop In Python W3schools String Operations In Python W3schools Do While Loop In Python W3schools W3schools Python Reverse String Is W3schools Enough To Learn Python Nlp Python Practice Https Www W3schools Com Python Python While Loop Asp Set Operations In Python W3schools What Are Arithmetic Operators In Python Python Switch Case With Strings