Find elements with relative locator strategy using Selenium 4 and JAVA

Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium-java version – 4.4.0
chrome browser version – 105
chrome driver version – 105 [used WebDriverManager library to auto download needed chrome driver version]

Chrome browser can be opened using selenium by following below steps:
1. Instantiate (creation of object) driver object with ChromeDriver() class using WebDriverManager library (This step opens the chrome browser)

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

2. Now, next steps of maximizing the browser, setting implicit timeout for driver object and launch of expected website can be carried out

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();
driver.get("https://www.google.com");

3. Now, find an element using find element method. Then use RelativeLocator class and methods on the found element to identify the adjacent elements. Then actions can be triggered on the elements that are identified through relative locators

//find the I'm feeling lucky button using By.cssselector strategy
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement imfeelingluckybutton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[role='search']>div>div>div[class]:not([style]) input[value*=' Feeling Lucky']")));

//find the google search button residing to left of "I'm feeling lucky" button using relative locator
WebElement googlesearchbuttonfoundusingrelativelocator = driver.findElement(RelativeLocator.with(By.tagName("input")).toLeftOf(imfeelingluckybutton));

//find the google search textbook residing above "I'm feeling lucky" button using relative locator
WebElement googlesearchtextboxfoundusingrelativelocator = driver.findElement(RelativeLocator.with(By.tagName("input")).above(imfeelingluckybutton));

//trigger actions on the elements found using relative locators
googlesearchtextboxfoundusingrelativelocator.sendKeys("test");
googlesearchbuttonfoundusingrelativelocator.click();

4. Quit/close the driver object after automating the necessary flows within the website.

driver.quit()

Sample code for reference

import io.github.bonigarcia.wdm.WebDriverManager;
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.locators.RelativeLocator;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.time.Duration;

public class FindAnElementWithRelativeLocatorStrategyUsingSelenium4 {

    public static void main(String[] args) throws IOException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        driver.manage().window().maximize();
        driver.get("https://www.google.com");

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement imfeelingluckybutton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[role='search']>div>div>div[class]:not([style]) input[value*=' Feeling Lucky']")));

        WebElement googlesearchbuttonfoundusingrelativelocator = driver.findElement(RelativeLocator.with(By.tagName("input")).toLeftOf(imfeelingluckybutton));
        WebElement googlesearchtextboxfoundusingrelativelocator = driver.findElement(RelativeLocator.with(By.tagName("input")).above(imfeelingluckybutton));

        googlesearchtextboxfoundusingrelativelocator.sendKeys("test");
        googlesearchbuttonfoundusingrelativelocator.click();

        driver.quit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *