-1

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

Meghasri
  • 103
  • 1
  • 7
  • 13
  • Please post the code you are using with any errors, etc. along with a link to the page and/or the relevant HTML. – JeffC Oct 04 '16 at 13:14
  • Possible duplicate of http://stackoverflow.com/questions/8244723/alert-handling-in-selenium-webdriver-selenium-2-with-java – JeffC Oct 04 '16 at 13:15
  • I could able to get the answer from the following link http://stackoverflow.com/questions/12893478/selenium-webdriver-with-java-cant-accept-alert As the code is not identifying the popup alert. I tried using Keys.ENTER to click on OK button. It worked for me – Meghasri Oct 04 '16 at 13:26

2 Answers2

2

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

  • 2nd line 'alert.accept();' will close the alert correct? – Meghasri Oct 04 '16 at 12:13
  • I used 1st line and driver.switchTo().alert(); and i gave the xpath for OK button but still it is not identifying the popup alert – Meghasri Oct 04 '16 at 12:14
  • I got an error like No alert is present (WARNING: The server did not provide any stacktrace information) – Meghasri Oct 04 '16 at 12:15
  • yes surely. It will work fine. you can refer below link. https://www.seleniumeasy.com/selenium-tutorials/how-to-handle-javascript-alerts-confirmation-prompts – Renuka Deshpande Oct 04 '16 at 12:16
  • no need to give xpath for OK button, if it is alert it will work. – Renuka Deshpande Oct 04 '16 at 12:17
  • @Meghasri Is it a javascript alert popup or something like a div made visible? – Grasshopper Oct 04 '16 at 12:38
  • actually the problem is, when i give 'driver.switchTo().alert();' then i could see the system is not identifiying it as alert at all. Any other solution :( ?? – Meghasri Oct 04 '16 at 12:45
  • Actual format for a popup should be something like this correct? But in my html code the format is different
    OK
    – Meghasri Oct 04 '16 at 13:08
0

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

Sai
  • 247
  • 2
  • 6
  • I agree that it's not an Alert but it's likely not a new window either. It sounds like a simple DIV popup. – JeffC Oct 04 '16 at 14:16
  • True. In which case the solution would be to simply wait for the ok button and click on it. But there isn't much info to go by. – Sai Oct 04 '16 at 14:20
  • Agreed. We, as a community, need to be more insistent on requiring basic facts before we start answering questions. We will get better questions, better answers, and spend a lot less time wasting time on questions with no info that will likely never be fixed into good questions. – JeffC Oct 04 '16 at 14:22