0
0
Selenium Javatesting~15 mins

findElement by partialLinkText in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify navigation using partial link text
Preconditions (2)
Step 1: Locate the link using partial link text 'Selenium'
Step 2: Click on the located link
Step 3: Wait for the new page to load
Step 4: Verify the URL contains 'selenium-automation'
Step 5: Verify the page title contains 'Selenium Automation'
✅ Expected Result: Clicking the link with partial text 'Selenium' navigates to the Selenium Automation page with correct URL and page title
Automation Requirements - Selenium WebDriver with JUnit 5
Assertions Needed:
Assert current URL contains 'selenium-automation'
Assert page title contains 'Selenium Automation'
Best Practices:
Use explicit waits to wait for page load
Use By.partialLinkText locator for finding the link
Use Page Object Model pattern for maintainability
Close the browser after test execution
Automated Solution
Selenium Java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class PartialLinkTextTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
        driver.get("https://example.com/homepage");
    }

    @Test
    public void testNavigateUsingPartialLinkText() {
        // Locate link by partial link text 'Selenium'
        WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selenium")));
        link.click();

        // Wait for URL to contain 'selenium-automation'
        wait.until(ExpectedConditions.urlContains("selenium-automation"));

        // Assert URL contains expected text
        String currentUrl = driver.getCurrentUrl();
        assertTrue(currentUrl.contains("selenium-automation"), "URL should contain 'selenium-automation'");

        // Assert page title contains expected text
        String title = driver.getTitle();
        assertTrue(title.contains("Selenium Automation"), "Title should contain 'Selenium Automation'");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test uses Selenium WebDriver with JUnit 5 to automate the manual test case.

Setup: We open Chrome browser and navigate to the homepage URL.

Test: We locate the link using By.partialLinkText("Selenium") which finds any link containing the word 'Selenium'. We wait until the link is clickable, then click it.

We then wait explicitly for the URL to contain 'selenium-automation' to ensure the page has loaded.

Assertions check that the current URL and page title contain the expected text, confirming correct navigation.

Teardown: The browser closes after the test to clean up.

This approach uses explicit waits to avoid timing issues and follows best practices for maintainability and reliability.

Common Mistakes - 3 Pitfalls
Using Thread.sleep() instead of explicit waits
Using By.linkText instead of By.partialLinkText when partial text is needed
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing with 3 different partial link texts to verify navigation for each.

Show Hint