⚙️ Basic Setup
Install and set up Selenium:
pip install selenium
Start a driver and open a page:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
๐ 1. Handling Links (<a> tags)
Links are usually <a> elements with an href attribute.
๐น Example HTML
<a href="https://example.com/home">Home</a>
<a href="https://example.com/about" id="aboutLink">About</a>
๐น Example Code
๐ Click a Link by Text
driver.find_element(By.LINK_TEXT, "Home").click()
๐ Click a Link by Partial Text
driver.find_element(By.PARTIAL_LINK_TEXT, "About").click()
๐ Click a Link by ID or XPath
driver.find_element(By.ID, "aboutLink").click()
# or using XPath
driver.find_element(By.XPATH, "//a[@href='https://example.com/about']").click()
๐ Get the Link URL
link = driver.find_element(By.LINK_TEXT, "Home")
print(link.get_attribute("href")) # prints full URL
๐ 2. Handling Buttons
Buttons can be of different forms:
<button> tag
<input type="button">
<input type="submit">
๐น Example HTML
<button id="submitBtn">Submit</button>
<input type="submit" value="Login" id="loginBtn">
๐น Example Code
๐ Click a Button by ID
driver.find_element(By.ID, "submitBtn").click()
๐ Click a Button by Text
driver.find_element(By.XPATH, "//button[text()='Submit']").click()
๐ Click a Button by CSS Selector
driver.find_element(By.CSS_SELECTOR, "input[value='Login']").click()
๐ Click a Button by Class Name
driver.find_element(By.CLASS_NAME, "btn-primary").click()
⏱ 3. Waiting for Links or Buttons (Best Practice)
Pages often load dynamically — so always wait for elements before interacting.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
# Wait for a button to be clickable
button = wait.until(EC.element_to_be_clickable((By.ID, "submitBtn")))
button.click()
๐งญ 4. Verify Link or Button Presence
# Check if link exists
links = driver.find_elements(By.LINK_TEXT, "Home")
if links:
print("Link found!")
else:
print("Link not found!")
# Check if button is enabled
button = driver.find_element(By.ID, "submitBtn")
if button.is_enabled():
print("Button is enabled")
๐งฉ 5. Advanced: Open Link in a New Tab
from selenium.webdriver.common.keys import Keys
link = driver.find_element(By.LINK_TEXT, "Home")
link.send_keys(Keys.CONTROL + Keys.RETURN) # Windows/Linux
# or Keys.COMMAND for macOS
๐ง 6. Common Locators for Links & Buttons
Locator Type Example When to Use
By.ID By.ID("submitBtn") When unique ID exists
By.NAME By.NAME("login") For named elements
By.LINK_TEXT By.LINK_TEXT("Home") For full visible link text
By.PARTIAL_LINK_TEXT By.PARTIAL_LINK_TEXT("About") For partial link text
By.XPATH By.XPATH("//button[@id='submitBtn']") Complex structures
By.CSS_SELECTOR By.CSS_SELECTOR(".btn-primary") When class or attribute is available
✅ Complete Example
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
driver = webdriver.Chrome()
driver.get("https://www.w3schools.com/html/html_forms.asp")
wait = WebDriverWait(driver, 10)
# Example: click a link
driver.find_element(By.LINK_TEXT, "HTML Tutorial").click()
# Example: go back
driver.back()
# Example: click a button (if present)
try:
button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Try it Yourself »']")))
button.click()
except:
print("Button not found")
driver.quit()
๐ Summary
Action Method
Click link by text By.LINK_TEXT / By.PARTIAL_LINK_TEXT
Click button .click() with By.ID, By.XPATH, etc.
Get link URL .get_attribute("href")
Wait before clicking WebDriverWait + EC.element_to_be_clickable()
Verify visibility .is_displayed() / .is_enabled()
Learn Selenium with JAVA Training in Hyderabad
Read More
Handling Checkboxes and Radio Buttons in Selenium
How to Verify Page Title and URL with Selenium
Navigating Between Pages Using Selenium WebDriver
How to Handle Input Forms in Selenium
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments