Question: Help me to find out the `xpath` for button "click for JS Alert"

Here is the link <https://the-internet.herokuapp.com/javascript_alerts>

I am using this `xpath`-

```
.//*[@id='content']/div/ul/li[1]/button 

```

and it showing no such element. 

This is the code used:

```
public void test() { 
    driver.get("the-internet.herokuapp.com/javascript_alerts");     
    WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']")); 
    element.click(); 
}

```

Answer: 
You use code in a wrong way:

```
WebElement element=driver.findElement(By.id("//button[text()='Click for JS Alert']")); 

```

`By.id` can be applied to search element with specified `id` attribute only while you use `XPath` instead. 

You need to use below:

```
WebElement element=driver.findElement(By.xpath("//button[text()='Click for JS Alert']")); 
element.click();

```
Try this below code.

```
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//button[contains(text(), 'Click for JS Alert')]")).click();
Thread.sleep(2500);
Alert alert = driver.switchTo().alert();
System.out.println(driver.switchTo().alert().getText());
alert.accept();

```