Selenium Java to locate a web element using the following and preceding XPath axes
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) { WebDriver driver; System.setProperty("webdriver.chrome.driver", "/home/codeaft/drivers/chromedriver"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.navigate().to("https://codeaft.github.io/testapp/"); //sending username driver.findElement(By.xpath("//*[@name='password']//preceding::input[@type='text']")).sendKeys("codeaft"); //sending password driver.findElement(By.xpath("//*[@name='username']//following::input[@type='password']")).sendKeys("codeaft"); //clicking login button driver.findElement(By.xpath("//*[@name='login']//self::button")).click(); driver.close(); } }
Comments and Reactions