Selenium Java script to locate web element by XPath using contains() and starts-with() methods
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) 
    {   
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.navigate().to("https://kodingwindow.com/testapp/");
        
        driver.findElement(By.xpath("//*[contains(@name,'ername')]")).sendKeys("kodingwindow");
        driver.findElement(By.xpath("//*[contains(@name,'passw')]")).sendKeys("kodingwindow");
        driver.findElement(By.xpath("//*[starts-with(@name,'log')]")).click();

        driver.close();
    }
}
Advertisement