How to Verify Page Title and URL with Selenium
How to Verify Page Title and URL with Selenium
✅ 1. Install Selenium
bash
Copy
Edit
pip install selenium
Make sure you have a WebDriver (e.g., ChromeDriver) installed and in your system PATH.
π§ͺ 2. Sample Code: Verify Title and URL
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
# Create a browser instance (e.g., Chrome)
driver = webdriver.Chrome()
# Navigate to a web page
driver.get("https://www.example.com")
# ✅ Verify the page title
expected_title = "Example Domain"
actual_title = driver.title
if actual_title == expected_title:
print("Title verification passed.")
else:
print(f"Title verification failed. Found: {actual_title}")
# ✅ Verify the URL
expected_url = "https://www.example.com/"
actual_url = driver.current_url
if actual_url == expected_url:
print("URL verification passed.")
else:
print(f"URL verification failed. Found: {actual_url}")
# Close the browser
driver.quit()
π§ Tips
Use .title to get the current page title.
Use .current_url to get the current page URL.
For partial matches, use in:
python
Copy
Edit
if "example" in driver.title:
print("Title contains 'example'")
π¦ Bonus: Using assert for Testing
python
Copy
Edit
assert driver.title == "Example Domain", "Title does not match!"
assert driver.current_url.startswith("https://"), "URL is not secure!"
Learn Selenium JAVA Course in Hyderabad
Read More
Navigating Between Pages Using Selenium WebDriver
How to Handle Input Forms in Selenium
Visit Our IHUB Talent Training in Hyderabad
Comments
Post a Comment