Selenium: Extract href by Element ID

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.

Advertisement

Previous Article

Kinematic Equation in C: Distance from u, t, and a

Next Article

Find a Character in a Given String in C

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨