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