I've written a selenium code with java testng for submitting a form. After clicking submit button the page navigates to thankyou page. But before loading thankyou am getting a Security Warning dialog box which has the options called 'Continue' and 'Cancel'. How to click Continue through selenium control. There is no way for getting xpath or id of the continue button.
问题:
回答1:
Had same problem, this worked for firefox 13 and selenium 2.0
Use spy to get the window info. For firefox 13 windows class is MozillaDialogClass WindowName is Security Warning.
declare import
[DllImport("user32.dll")]
public static extern int FindWindow(string className, string windowName);
make method
public static void SetOkButton(string className, string windowName)
{
int handle = FindWindow(className, windowName);
if (handle > 0)
{
if (SetForegroundWindow(handle))
{
SendKeys.SendWait("{ENTER}");
}
}
}
call the method
SetOkButton("MozillaDialogClass", "Security Warning");
回答2:
Remember to add a wait before it appears otherwise your code may executed before alert actually appears. Following piece of code is working for me
private void acceptSecurityAlert() {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(3, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
Alert alert = wait.until(new Function<WebDriver, Alert>() {
public Alert apply(WebDriver driver) {
try {
return driver.switchTo().alert();
} catch(NoAlertPresentException e) {
return null;
}
}
});
alert.accept();
}
回答3:
If that is a JS confirmation box on load of the page, then (as the docs say), you can't do much. Selenium 2 (WebDriver) can handle these dialogs in a much better way:
driver.switchTo().alert().accept();
If that is a Firefox confirmation, you can't do anything with Selenium.
I'd give java.awt.Robot a try.
回答4:
There is no xpath or id of the 'Continue' button on the 'Security Warning' dialog as this dialog is not a browser dialog. This dialog was generated by Java.
Selenium only automates browsers. Hence, it is beyond the scope of Selenium to click on the 'Continue' button.
Thankfully there is a way to click on the 'Continue' button by taking the help of these jars: jna.jar and jna-platform.jar and java.awt.Robot class.
If you don't know when 'Security Warning' will appear, you can write code to wait until the 'Security Warning' appears. This code keeps checking for the current active window title. Once 'Security Warning' dialog appears, the currently active window will become 'Security Warning' dialog. The code then uses TAB key to navigate to 'Continue' button and presses ENTER key.
You can use the below method after doing the necessary imports:
public void acceptSecurityAlert() {
//Keep checking every 7 seconds for the 'Security Warning'
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(1000, TimeUnit.SECONDS).pollingEvery(7, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
//Wait until the 'Security Warning' appears
boolean isTrue = wait.until(new Function<WebDriver, Boolean>(){
//implement interface method
public Boolean apply(WebDriver driver) {
try {
char[] buffer = new char[1024 * 2];
HWND hwnd = User32.INSTANCE.GetForegroundWindow();
User32.INSTANCE.GetWindowText(hwnd, buffer, 1024);
//System.out.println("Active window title: " + Native.toString(buffer));
//Check for 'Security Warning' window
if(Native.toString(buffer).equalsIgnoreCase("Security Warning")){
//After 'Security Warning' window appears, use TAB key to go to 'Continue' button and press ENTER key.
//System.out.println("Pressing keys...");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB); robot.delay(200);
robot.keyRelease(KeyEvent.VK_TAB); robot.delay(200);
robot.keyPress(KeyEvent.VK_TAB); robot.delay(200);
robot.keyRelease(KeyEvent.VK_TAB); robot.delay(200);
robot.keyPress(KeyEvent.VK_ENTER); robot.delay(200);
robot.keyRelease(KeyEvent.VK_ENTER); robot.delay(200);
return true;
}
return null;
}catch(Exception e) {
System.out.println("Exception!");
e.printStackTrace();
}
return null;
}
});
}