首页 \ 问答 \ 如何使用机器人框架接受警报(How to accept an alert using robot framework)

如何使用机器人框架接受警报(How to accept an alert using robot framework)

使用selenium2library通过机器人框架自动化我的Web应用程序。 我无法使用现有关键字接受浏览器提醒。

尝试接受警报时获取以下异常。

UnexpectedAlertPresentException: Alert Text: Delete selected trusted provider(s)?
<super: <class 'WebDriverException'>, <UnexpectedAlertPresentException object>>

尝试过以下selenium2library关键词Alert Should Be PresentGet Alert MessageConfirm Action

请指导我如何继续。


Using selenium2library to automate my web application with robot framework. I am not able to accept a browser alert using the existing keywords.

Getting the below exception while trying to accept the alert.

UnexpectedAlertPresentException: Alert Text: Delete selected trusted provider(s)?
<super: <class 'WebDriverException'>, <UnexpectedAlertPresentException object>>

have tried the below selenium2library keywords Alert Should Be Present, Get Alert Message , Confirm Action, etc.

Please guide me on how to proceed.


原文:https://stackoverflow.com/questions/26521238
更新时间:2022-06-27 22:06

最满意答案

在你的javascript中,你正在劫持表单以便它通过ajax提交,但是你在表单上调用replaceWith,所以你被劫持的表单被删除并被一个新的,未被劫持的表单替换。 要解决这个问题,你也可以

1)只替换表单的内容 - 这应该有效,因为您只是将事件附加到表单本身而不是其子元素

2)将您的js编写为一个函数,您可以先在初始表单上调用,然后在通过ajax加载的任何新表单上调用。

更新:例如,

<script type="text/javascript"> 
$(document).ready(function(){

    function hijack() {
        var form = $('form#createrecipeform');
        form.submit(function(e) {
            e.preventDefault();
            console.log('ajax form submission function called successfully.');
            //form = $(this);
            console.log(form)
            var serialized_form = form.serialize();
            $.ajax({ type: "POST", 
                url: $(this).attr('action'),
                data: serialized_form, 
                success: (function(data) { 
                    console.log('ajax success function called successfully.');
                    data = $.parseJSON(data);
                    if (data.success) {
                        console.log('success');
                    } else {        
                        console.log('failure');
                        var newForm = data.form;
                        form.replaceWith(newForm);
                        hijack();
                    }
                })
            });
            return false;
        });
    };

    hijack();

});
</script> 

In your javascript, you're hijacking the form so that it submits via ajax, but then you're calling replaceWith on the form, so your hijacked form gets obliterated and is replaced with a new, non-hijacked form. To solve this you can either

1) Only replace the content of the form - this should work since you're only attaching events to the form itself and not its child elements

2) Write your js as a function which you can call firstly on the initial form, and subsequently on any new forms loaded via ajax.

UPDATE: for example,

<script type="text/javascript"> 
$(document).ready(function(){

    function hijack() {
        var form = $('form#createrecipeform');
        form.submit(function(e) {
            e.preventDefault();
            console.log('ajax form submission function called successfully.');
            //form = $(this);
            console.log(form)
            var serialized_form = form.serialize();
            $.ajax({ type: "POST", 
                url: $(this).attr('action'),
                data: serialized_form, 
                success: (function(data) { 
                    console.log('ajax success function called successfully.');
                    data = $.parseJSON(data);
                    if (data.success) {
                        console.log('success');
                    } else {        
                        console.log('failure');
                        var newForm = data.form;
                        form.replaceWith(newForm);
                        hijack();
                    }
                })
            });
            return false;
        });
    };

    hijack();

});
</script> 

相关问答

更多
  • Submit是一个HTML表单结构...您必须使用表单对象的名称属性,如下所示...在您的模板中:
    ...
    ...
    在你看来: if 'list' in request.POST: ...
  • 您应该使用link_to 。 <%= link_to "Cancel", { controller: "my_objects", action: "index", class: "btn" } %> You should be using a link_to instead. <%= link_to "Cancel", { controller: "my_objects", action: "index", class: "btn" } %>