In today’s digital age, automation is more than just a convenience—it’s a necessity. Whether you're a software tester, data analyst, or developer, the ability to automate web browsing can save countless hours. In this Selenium with Python Tutorial, brought to you by Tpoint Tech, we’ll help you master browser automation using one of the most powerful tools available: Selenium.
What is Selenium?
Selenium is a widely-used open-source framework for automating web browsers. Initially developed for testing web applications, Selenium is now used for a variety of browser-based tasks, such as testing, scraping, and robotic process automation (RPA).
It supports multiple browsers like Chrome, Firefox, Safari, and Edge, and works across operating systems. Selenium also supports multiple programming languages—Python being one of the most popular due to its simplicity and efficiency.
Why Selenium with Python?
Selenium’s compatibility with Python makes it the ideal tool for automation. Here's why:
- Python is beginner-friendly and easy to read.
- It has excellent support libraries and tools for web scraping and testing.
- Python integrates well with Selenium, making it ideal for quick prototyping and automation.
So whether you're automating login processes or scraping thousands of product listings, Selenium with Python has you covered.
Setting Up Selenium with Python
Before jumping into the code, let’s set up your environment.
1.Install Python
Download and install Python from python.org. Ensure the option “Add Python to PATH” is selected during installation.
2.Install Selenium Library
Use pip to install Selenium:
pip install selenium
3.Download WebDriver
WebDriver acts as the bridge between Selenium and your browser. For Chrome, download ChromeDriver from the official site. Make sure the version matches your Chrome browser.
Your First Selenium Script
Let’s automate a simple task: searching for Tpoint Tech on Google.
Code Example: Google Search Automation
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# Set up Chrome browser
driver = webdriver.Chrome()
# Open Google
driver.get("https://www.google.com")
# Find the search box, type in query, and submit
search = driver.find_element("name", "q")
search.send_keys("Tpoint Tech")
search.send_keys(Keys.RETURN)
# Wait for results to load
time.sleep(2)
# Extract and print search result titles
results = driver.find_elements("tag name", "h3")
for result in results:
print(result.text)
# Close browser
driver.quit()
This basic script demonstrates how to launch a browser, perform a search, and extract content from a web page using Selenium.
Automating Website Login
Want to automate logging into a website? Selenium can easily fill forms and click buttons.
Code Example: Automate Login Form
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get("https://example.com/login")
# Enter credentials
driver.find_element(By.ID, "username").send_keys("myusername")
driver.find_element(By.ID, "password").send_keys("mypassword")
# Submit form
driver.find_element(By.ID, "login-button").click()
# Optional wait to see results
time.sleep(3)
driver.quit()
Make sure to inspect the webpage (right-click → Inspect) to get the right element IDs or class names.
Advanced: Using Headless Mode
Want to run scripts without opening a browser window? Use headless mode:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)
driver.get("https://www.tpoint.tech")
print(driver.title)
driver.quit()
Headless mode is perfect for background automation tasks or when running scripts on a server.
Tips for Writing Better Selenium Scripts
Here are a few best practices from Tpoint Tech:
✅ Use Explicit Waits
Replace time.sleep()
with smarter waits:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "some-id"))
)
✅ Handle Exceptions Gracefully
Use try-except
blocks to avoid crashes when elements aren’t found.
✅ Organize Code with Functions or Classes
For large scripts, organize your code into reusable functions or class methods.
✅ Always Close the Browser
Use driver.quit()
or wrap your logic in a try-finally
block.
Conclusion
In this Selenium with Python Tutorial, we covered:
- What is Selenium?
- How to install and set up Selenium with Python
- Writing basic automation scripts
- Automating form submissions
- Advanced usage like headless mode and best practices
Selenium opens up endless possibilities for automation, testing, and scraping. With Python as your companion, browser automation becomes intuitive and powerful.
Stay connected with Tpoint Tech for more tutorials, tech tips, and deep dives into automation tools. Whether you’re a beginner or a pro, we’re here to guide your automation journey—one script at a time.
Top comments (0)