Question: I tried to switch to popup alert and click on OK button, but i got an error saying that xpath (for OK button) is not found.

But this is working for me sometimes using the same code. Could anyone help me out on this. I tried all possible ways that is available in blogs. But i couldn't make it

Answer: 
you need to move control to your pop-up window first before doing any operation on pop-up window:-

below code is to move selenium control on pop-up window

```
driver.switchTo().alert();

```

by writing below line

```
alert.accept();

```

alert will get close
Based on the original question and the subsequent comments, I suspect what you're dealing with is a browser pop up window and not an alert. So this won't work

```
driver.switchTo().alert().accept();

```

You need to use window handles

```
Set<String> handles = driver.getWindowHandles(); 
Iterator<String> windows = handles.iterator(); 
String parent = windows.next(); 
String child = windows.next();
driver.switchTo().window(child);
driver.findElement(By.xpath("insert xpath to OK button")).click();
driver.switchTo().window(parent);
//continue with steps on parent window

```

Note: ensure that you add the required synchronization to the code snippet above