如何使用Selenium单击“安全警告”对话框中的选项? [英] How to Click a option in Security Warning dialog box using Selenium?

查看:312
本文介绍了如何使用Selenium单击“安全警告”对话框中的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用java testng编写了一个selenium代码来提交表单。单击提交按钮后,页面将导航到thankyou页面。但是在加载之前,我会收到一个安全警告对话框,其中包含名为继续和取消的选项。如何单击继续通过selenium控件。无法获取继续按钮的xpath或id。

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.

推荐答案

请记住在出现之前添加等待,否则您的代码可能在警报实际出现之前执行。以下代码段正在为我工​​作

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

这篇关于如何使用Selenium单击“安全警告”对话框中的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆