Selenium Java to locate a web element using the last() method
KW.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class KW 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("https://kodingwindow.com/testapp/");
        
        driver.findElement(By.xpath("(//input)[last()-1]")).sendKeys("kodingwindow");
        driver.findElement(By.xpath("(//input)[last()]")).sendKeys("kodingwindow");
        
        driver.findElement(By.xpath("(//button)[last()]")).click();
        driver.close();
    }
}
Advertisement