Last modified: Nov 21, 2023 By Alexander Williams
Python Selenium: Get URL After Click - Examples
Example 1: Retrieve Current URL After Click
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Click an element (e.g., a button)
button = driver.find_element_by_xpath('//button[@id="example-button"]')
button.click()
# Retrieve the current URL
current_url = driver.current_url
print(f"The current URL after clicking is: {current_url}")
# Close the browser
driver.quit()
Output:
# The current URL after clicking is: https://example.com/updated-url
Example 2: Use get_attribute('href') for Anchor Elements
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Click an anchor element (e.g., a link)
link = driver.find_element_by_xpath('//a[@id="example-link"]')
link.click()
# Retrieve the href attribute to get the URL
clicked_url = link.get_attribute('href')
print(f"The URL after clicking is: {clicked_url}")
# Close the browser
driver.quit()
Output:
# The URL after clicking is: https://example.com/new-page
Example 3: Use WebDriverWait for Dynamic Page Changes
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Click an element triggering dynamic page changes
dynamic_element = driver.find_element_by_xpath('//div[@id="dynamic-element"]')
dynamic_element.click()
# Wait for the URL to change (adjust timeout as needed)
new_url = WebDriverWait(driver, 10).until(
EC.url_changes(driver.current_url)
)
print(f"The URL after clicking the dynamic element is: {new_url}")
# Close the browser
driver.quit()
Output:
# The URL after clicking the dynamic element is: https://example.com/dynamic-page
Example 4: Use JavaScript to Click and Retrieve URL
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage
driver.get('https://example.com')
# Click an element using JavaScript
element_to_click = driver.find_element_by_xpath('//div[@id="js-clickable-element"]')
driver.execute_script("arguments[0].click();", element_to_click)
# Retrieve the current URL
current_url = driver.current_url
print(f"The current URL after JavaScript click is: {current_url}")
# Close the browser
driver.quit()
Output:
# The current URL after JavaScript click is: https://example.com/js-clicked-page
Example 5: Handle Multiple Windows or Tabs
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Open a new window or tab (Ctrl+Click or Command+Click)
driver.execute_script("window.open('https://example.com/new-window', '_blank');")
# Switch to the new window or tab
driver.switch_to.window(driver.window_handles[1])
# Retrieve the current URL in the new window or tab
new_url = driver.current_url
print(f"The URL in the new window or tab is: {new_url}")
# Close the browser
driver.quit()
Output:
# The URL in the new window or tab is: https://example.com/new-window
Example 6: Handling Redirects and Pop-ups
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with a redirect or pop-up
driver.get('https://example.com/redirect-page')
# Click an element triggering the redirect or pop-up
redirect_element = driver.find_element_by_xpath('//button[@id="redirect-button"]')
redirect_element.click()
# Wait for the new URL (adjust timeout as needed)
new_url = WebDriverWait(driver, 10).until(
EC.url_contains('redirected-page')
)
print(f"The URL after handling redirects is: {new_url}")
# Close the browser
driver.quit()
Output:
# The URL after handling redirects is: https://example.com/redirected-page