selenium-webdriverxpathselenium-java

find an element by Xpath using selenium Java


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class SeleniumBasics {
    
public static void main(String[] args) {  
        
        WebDriver driver= new ChromeDriver();  
        driver.get("https://formy-project.herokuapp.com/");
        driver.manage().window().maximize(); 
        driver.findElement(By.xpath("//body/div[1]/div[1]/li[1]")).click();
        
    }  

}

Im trying to click on the element "autocomplete" in this page https://formy-project.herokuapp.com/...not working ..any help pleae?

Absolute Xpath relative Xpath find by element


Solution

  • //body/div[1]/div[1]/li[1]
    

    You are locating the <li> element. You should be locating the <a> element.

    Also don't use absolute XPath. Use relative XPath as below.

    //a[@class='btn btn-lg' and text()='Autocomplete']