Navigating Between Pages Using Selenium WebDriver
Navigating Between Pages Using Selenium WebDriver
✅ What You’ll Need:
Python installed on your system
Selenium package installed (pip install selenium)
WebDriver for your browser (e.g., ChromeDriver for Chrome)
π Basic Page Navigation with Selenium
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Set up the WebDriver (e.g., Chrome)
driver = webdriver.Chrome()
# 1. Open the first page
driver.get("https://www.example.com")
print("Title of first page:", driver.title)
# 2. Click a link to go to another page (assuming there's a clickable element)
try:
link = driver.find_element(By.LINK_TEXT, "More information...")
link.click()
time.sleep(2) # wait for page to load
print("Title after navigating:", driver.title)
except Exception as e:
print("Error clicking link:", e)
# 3. Go back to the previous page
driver.back()
time.sleep(2)
print("Title after going back:", driver.title)
# 4. Go forward to the next page
driver.forward()
time.sleep(2)
print("Title after going forward:", driver.title)
# 5. Refresh the current page
driver.refresh()
print("Page refreshed")
# Close the browser
driver.quit()
π Key Selenium Navigation Methods:
Method Description
driver.get(url) Opens the given URL
driver.back() Navigates back in browser history
driver.forward() Navigates forward in browser history
driver.refresh() Refreshes the current page
Would you like to see an example using buttons or dropdowns for navigation instead of links?
Learn Selenium JAVA Course in Hyderabad
Read More
How to Handle Input Forms in Selenium
Working with ChromeDriver, GeckoDriver, and EdgeDriver
Visit Our IHUB Talent Training in Hyderabad
Comments
Post a Comment