简体   繁体   English

如何使用Selenium单击“安全警告”对话框中的选项?

[英]How to Click a option in Security Warning dialog box using Selenium?

I've written a selenium code with java testng for submitting a form. 我用java testng编写了一个selenium代码来提交表单。 After clicking submit button the page navigates to thankyou page. 单击提交按钮后,页面将导航到thankyou页面。 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. 如何单击继续通过selenium控件。 There is no way for getting xpath or id of the continue button. 无法获取继续按钮的xpath或id。

Had same problem, this worked for firefox 13 and selenium 2.0 有同样的问题,这适用于firefox 13和selenium 2.0

Use spy to get the window info. 使用间谍获取窗口信息。 For firefox 13 windows class is MozillaDialogClass WindowName is Security Warning. 对于firefox 13 windows类是MozillaDialogClass WindowName是安全警告。

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");

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();
}

If that is a JS confirmation box on load of the page, then (as the docs say ), you can't do much. 如果这是加载页面的JS确认框,那么(正如文档所说 ),你做不了多少。 Selenium 2 ( WebDriver ) can handle these dialogs in a much better way: Selenium 2( WebDriver )可以更好的方式处理这些对话框:

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

If that is a Firefox confirmation, you can't do anything with Selenium. 如果这是Firefox确认,则无法对Selenium执行任何操作。

I'd give java.awt. 我给java.awt。 Robot a try. 机器人一试。

There is no xpath or id of the 'Continue' button on the 'Security Warning' dialog as this dialog is not a browser dialog. “安全警告”对话框中没有“继续”按钮的xpath或id,因为此对话框不是浏览器对话框。 This dialog was generated by Java. 该对话框由Java生成。

Selenium only automates browsers. Selenium只能自动化浏览器。 Hence, it is beyond the scope of Selenium to click on the 'Continue' button. 因此,单击“继续”按钮超出了Selenium的范围。

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. 值得庆幸的是,有一种方法可以通过获取这些罐子来点击“继续”按钮:jna.jar和jna-platform.jar以及java.awt.Robot类。

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. 然后代码使用TAB键导航到“继续”按钮并按ENTER键。

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;
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM