Beginner 8 min 7 scenarios

Links Automation Practice

Master link interactions in Selenium & Playwright — internal links, external links, broken links, image links, button links, anchor navigation, and HTTP status code assertions.

Scenario 1: Internal Following Links

Click an internal link and assert the URL changes within the same tab.


Scenario 2: External Links (New Tab)

Assert target="_blank" and switch to the new window handle after clicking.


Scenario 3: Broken Links

Send an HTTP GET to the link's href and assert the response status is 4xx/5xx.


Scenario 4: Image Links

Click a link that wraps an image and assert navigation. Check alt text for accessibility.

Broken image linkIron Man

Scenario 5: Button Links

Locate a <button> wrapped in a link and assert both click behaviour and navigation.


Scenario 6: Text Links & Anchor

Assert link text with getText(). Test same-page anchor navigation.

↑ Anchor target reached

Scenario 7: API Status Code Links

Click a button, intercept the HTTP response, and assert the status code matches the expected value.

Click a button above to see the HTTP status code.

What You'll Learn

Selenium (Java)

  • click()
  • getAttribute("href")
  • getAttribute("target")
  • driver.navigate().back()
  • driver.getWindowHandles()
Tutorial video coming soon

Test Cases

Introduction

Links (<a> tags) are fundamental navigation elements. Automation scenarios include:

  1. Click a link — navigate to another page
  2. Find link by text — locate by visible text
  3. Verify the href — assert the destination URL
  4. Handle new tab — link opens in a new window/tab
  5. Verify broken links — check HTTP status codes

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Click link findElement(By.linkText(...)).click() getByRole("link", { name: ... }).click() get_by_role("link", name=...).click()
By partial text By.partialLinkText(...) getByText(...) get_by_text(...)
Get href getAttribute("href") getAttribute("href") get_attribute("href")
New tab switch to window handle context.waitForEvent("page") context.wait_for_event("page")
Current URL getCurrentUrl() page.url() page.url

Selenium (Java)

driver.findElement(By.linkText("Click Me")).click();

Playwright (JS)

await page.getByRole("link", { name: "Click Me" }).click();

Playwright (Python)

page.get_by_role("link", name="Click Me").click()

Selenium (Java)

driver.findElement(By.partialLinkText("Click")).click();

Playwright (JS)

await page.getByText("Click").click();

Playwright (Python)

page.get_by_text("Click").click()

3. Verify href attribute

Selenium (Java)

String href = driver.findElement(By.id("externalLink")).getAttribute("href");
assertTrue(href.contains("qaplayground.com"));

Playwright (JS)

const href = await page.locator("#externalLink").getAttribute("href");
expect(href).toContain("qaplayground.com");

Playwright (Python)

href = page.locator("#externalLink").get_attribute("href")
assert "qaplayground.com" in href

Selenium (Java)

String originalWindow = driver.getWindowHandle();
 
driver.findElement(By.id("newTabLink")).click();
 
// Wait for new window and switch to it
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
 
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(originalWindow)) {
        driver.switchTo().window(handle);
        break;
    }
}
 
System.out.println("New tab URL: " + driver.getCurrentUrl());
driver.close();
driver.switchTo().window(originalWindow);

Playwright (JS)

const [newPage] = await Promise.all([
  page.context().waitForEvent("page"),
  page.locator("#newTabLink").click(),
]);
await newPage.waitForLoadState();
console.log("New tab URL:", newPage.url());
await newPage.close();

Playwright (Python)

with page.context.expect_page() as new_page_info:
    page.locator("#newTabLink").click()
new_page = new_page_info.value
new_page.wait_for_load_state()
print("New tab URL:", new_page.url)
new_page.close()

5. Verify current URL after navigation

Selenium (Java)

driver.findElement(By.id("homeLink")).click();
assertEquals("https://www.qaplayground.com/", driver.getCurrentUrl());

Playwright (JS)

await page.locator("#homeLink").click();
await expect(page).toHaveURL("https://www.qaplayground.com/");

Playwright (Python)

page.locator("#homeLink").click()
expect(page).to_have_url("https://www.qaplayground.com/")

Selenium (Java)

List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links: " + links.size());

Playwright (JS)

const count = await page.locator("a").count();
console.log("Total links:", count);

Playwright (Python)

count = page.locator("a").count()
print("Total links:", count)

📄 Also Read: Top 10 Best Automation Practice Website

Frequently Asked Questions