Selenium Java to locate a web element using the last() method
Codeaft.java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; class Codeaft { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/home/codeaft/drivers/chromedriver"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://codeaft.github.io/testapp/"); driver.findElement(By.xpath("(//input)[last()-1]")).sendKeys("codeaft"); driver.findElement(By.xpath("(//input)[last()]")).sendKeys("codeaft"); driver.findElement(By.xpath("(//button)[last()]")).click(); driver.close(); } }
Comments and Reactions