0
0
Selenium Javatesting~10 mins

findElement by partialLinkText in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page and finds a link using a part of its visible text. It then clicks the link and verifies that the new page contains expected content.

Test Code - JUnit
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PartialLinkTextTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testFindElementByPartialLinkText() {
        driver.get("https://example.com");
        WebElement link = driver.findElement(By.partialLinkText("More information"));
        link.click();
        String pageSource = driver.getPageSource();
        assertTrue(pageSource.contains("Example Domain"), "Page should contain 'Example Domain'");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Browser navigates to https://example.comPage loads showing Example Domain homepage-PASS
3Find element by partial link text 'More information'Link with text containing 'More information' is locatedElement is found and not nullPASS
4Click the found linkBrowser navigates to the linked page-PASS
5Get page source and check it contains 'Example Domain'Page source is retrievedAssert page source contains 'Example Domain'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The link with partial text 'More information' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What method is used to find the link in this test?
AfindElement(By.id("More information"))
BfindElement(By.className("More information"))
CfindElement(By.partialLinkText("More information"))
DfindElement(By.tagName("a"))
Key Result
Using partialLinkText locator helps find links when you know only part of the link text, making tests more flexible and less brittle.