Written: August 22, 2026
Once you locate an anchor by ID, getAttribute(“href”) (Java/Python) or get_attribute(“href”) returns the link. Prefer explicit waits so you are not reading before the node exists.
Python example
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
el = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "docs-link"))
)
href = el.get_attribute("href")
print(href)
Notes
Browsers may return an absolute URL even if the HTML has a relative href — assert against what your app actually needs.
Keep learning
If this walkthrough on selenium get href by id helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.
Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.