How to Handle Links in Selenium and Playwright
Master link interactions in Selenium & Playwright โ internal links, external links, broken links, image links, button links, anchor navigation, and HTTP status code assertions.
Beginner8 min7 scenarios12 test cases
Interactive Scenarios
S02Scenario 2: External Links (New Tab)
Click an external link
S03Scenario 3: Broken Links
S04Scenario 4: Image Links
Click an image link
S06Scenario 6: Text Links & Anchor
Homdf56eAbout 32 yhsTest Links Page using Selenium Webdriver with Java and C#Links Anchor Text โ Test Cases TC09
Click a text or anchor linkโ Anchor target reached
S07Scenario 7: API Status Code Links
Click an API link
Showing 12 test cases
| ID | Scenario | Type | Priority | |
|---|---|---|---|---|
| LNK_001 | Verify link navigates to the correct URL on click Expected: Page URL should update to the target URL. โ
positivehigh | positive | high | |
| LNK_002 | Verify link text matches expected label Expected: Link text should match the expected visible text. โ
positivemedium | positive | medium | |
| LNK_003 | Verify external link opens in a new tab Expected: A new window/tab should open with the correct URL. โ
positivehigh | positive | high | |
| LNK_004 | Verify internal link stays in the same tab Expected: Navigation should occur in the same window/tab. โ
positivehigh | positive | high | |
| LNK_005 | Verify broken link returns HTTP error status Expected: An HTTP request to the link's href should return an error status (e.g., 404, 500). โ negativehigh | negative | high | |
| LNK_006 | Verify link is keyboard accessible Expected: Link can be focused and activated via keyboard. โ
positivemedium | positive | medium | |
| LNK_007 | Verify link href attribute contains the correct URL Expected: The href attribute must contain the exact expected URL. โ
positivehigh | positive | high | |
| LNK_008 | Verify link has accessible label for screen readers Expected: Link should have an aria-label or descriptive text. โ
positivemedium | positive | medium | |
| LNK_009 | Verify link hover state is visually distinct Expected: CSS styles should change when the link is hovered. โ
positivelow | positive | low | |
| LNK_010 | Verify right-click on link shows browser context menu Expected: Right-clicking should open the context menu without custom interference. โ
positivelow | positive | low | |
| LNK_011 | Verify link with dynamic URL resolves correctly Expected: Dynamically generated links should lead to the correct specific resource. โ
positivehigh | positive | high | |
| LNK_012 | Verify link page loads without console errors Expected: Clicking a link and navigating should not produce JavaScript errors. โ ๏ธ edgemedium | edge | medium |
Overview
Links (<a> tags) are fundamental to web navigation. Testing them ensures users can seamlessly traverse your application and access external resources.
1 ยท Internal & External
Internal links navigate within the same application context, whereas external links often open in a new tab (target='_blank'). Frameworks handle new tabs differently.
TypeScript
// Click standard link
await page.locator('#my-link').click();
// Handle new tab
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.locator('#external-link').click()
]);
await newPage.waitForLoadState();2 ยท Checking Status Codes
Instead of clicking every link, you can verify a link isn't broken by making a fast HTTP request to its href attribute and checking the response code.
TypeScript
const href = await page.locator('#my-link').getAttribute('href');
const response = await page.request.get(href);
expect(response.status()).toBe(200);Method Summary
| Action | Selenium | Playwright JS | Playwright PY | Cypress |
|---|---|---|---|---|
| Click Link | element.click() | locator.click() | locator.click() | .click() |
| Get URL | element.getAttribute("href") | locator.getAttribute("href") | locator.get_attribute("href") | .invoke('attr', 'href') |
| Check Target | element.getAttribute("target") | locator.getAttribute("target") | locator.get_attribute("target") | .invoke('attr', 'target') |
FAQ
