2

I have an web application, which generates a JS message 'Are you sure you want to navigate away from this page?' when you try to open a new page. I know I can handle this message by

Selenium.getAlert();

(or some modification of it, I haven't tried it yet)

But my main problem is that this message generates only when I leave this page. In selenium I can leave page by using

Selenium.open("new address");

or

Selenium.back();

So I use code like this

Selenium.open("new address");
Selenium.getAlert();

But the problem is that Selenium.open doesn't finish and go to next code line in program until the new page is fully loaded, but the page can't be loaded until program goes to next code line and handles this alert. So it's ablocked situation and I don't know how to handle it.

Dogmatixed
  • 794
  • 1
  • 11
  • 33
ShockwaveNN
  • 2,227
  • 2
  • 29
  • 56

3 Answers3

1

I don't think it can be done, staying entirely within the Selenium RC API. I have cases similar to this that I handle by launching an AutoIt script, before the open(), that waits for the prompt and answers it. That only works on Windows, but if you need something for other systems, I'm sure there are equivalent tools.

Selenium has always had a problem with alerts and confirmations (which this is - a confirmation has an OK/Cancel choice) that occur duing page loading. There's even an ancient bug number enshrined in one of the error messages that explains that it can't catch them.

Ross Patterson
  • 9,527
  • 33
  • 48
  • Yep, i also get to idea to use AutoIt script to handle this situation. It's not pretty and only on windows, but it's work – ShockwaveNN Aug 30 '11 at 08:49
0

I don't know if this will work, but it's an idea. You could try something like:

try {
    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    Selenium.open("new address");
} catch (Exception e) {
    // Should throw after 1 second
}

// Now we may be able to interact with the alert.
Selenium.getAlert();
joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
0

Use:

openAndWait(..)

Maybe this other SO question will help you.

Community
  • 1
  • 1
Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40