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

        driver.close();
    }
}
Comments and Reactions