Capturing Page Titles and URLs in Selenium Python
Selenium is a powerful tool for automating web browsers using different programming languages, and Python is one of the most popular choices due to its readability and simplicity. One of the fundamental tasks when working with Selenium is capturing the page title and URL of the current webpage. This is especially useful in test automation scenarios where you want to verify that a user has landed on the correct page or ensure navigation works as expected.
In this blog, we’ll walk you through how to capture page titles and URLs using Selenium with Python, along with some real-world use cases and best practices.
Setting Up Selenium with Python
Before you begin, ensure you have Selenium installed. You can install it using pip:
bash
pip install selenium
You'll also need a web driver (like ChromeDriver or GeckoDriver) compatible with the browser you’re using.
Importing Required Libraries
Here’s a basic setup using Chrome WebDriver:
python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
Using webdriver_manager is a convenient way to manage driver binaries automatically.
Capturing Page Title and URL
Here’s a complete example:
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
# Initialize the browser
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Open a website
driver.get("https://www.python.org")
# Wait for the page to load
time.sleep(2)
# Get the page title
page_title = driver.title
print("Page Title:", page_title)
# Get the current URL
current_url = driver.current_url
print("Current URL:", current_url)
# Close the browser
driver.quit()
Explanation
driver.title: Retrieves the title of the current web page.
driver.current_url: Fetches the URL of the currently loaded page.
These two commands are crucial for asserting correct navigation in automated tests.
Real-World Use Cases
- Navigation Validation: After clicking a button or link, you can verify that the user is redirected to the correct page by checking the title or URL.
- SEO Testing: Ensure that each page has the appropriate title for search engines.
- Broken Link Checks: Navigate through links and confirm the resulting pages load correctly and return expected titles/URLs.
- Multi-Page Forms: In multi-step forms, validate that each step leads to the right destination.
Using Assertions for Validation
In test scripts, you can combine these with assertions:
python
Copy
Edit
assert "Welcome to Python.org" in driver.title
assert driver.current_url == "https://www.python.org/"
This helps automate quality checks in CI/CD pipelines.
Best Practices
- Avoid Hard-Coding URLs: Use config files or environment variables.
- Add Waits: Use WebDriverWait instead of sleep() for better reliability.
- Use Logging: Instead of print(), use logging libraries for better test traceability.
Final Thoughts
Capturing the page title and URL in Selenium with Python is a simple yet essential skill for anyone working with web automation or testing. It not only helps in navigation validation but also ensures better reliability and accuracy of your automated test cases.
Learn Selenium with Pyhton Training Hyderabad
Read More: Logging In to Websites Using Selenium and Python
Get Direction
Comments
Post a Comment