Selenium:用户关闭弹出窗口后切换到主窗口

时间:2017-08-25 14:01:06

标签: python selenium popupwindow

我有一个应用程序,用户可以单击按钮并弹出窗口显示。然后,在此弹出窗口中单击“提交”按钮后,窗口将关闭,用户将继续在主窗口中工作。

我希望能够在主窗口中使用selenium选择元素,然后在弹出窗口中选择元素,然后在主窗口中再次关闭弹出窗口。任何想法我将如何做到这一点?

修改
这是我的代码。我正在使用django-addanother创建带弹出窗口的表单。主要形式是Robject形式。休息表格是弹出窗口。

class CreateRobjectView(PermissionRequiredMixin, CreateView):
    template_name = 'robjects/create_robject.html'
    success_url = "/projects/proj_1/robjects/"
    raise_exception = True
    permission_required = ['projects.can_visit_project',
                           'projects.can_modify_project']

    def get(self, request, *args, **kwargs):
        Name.objects.filter(robjects=None).delete()
        RobjectFile.objects.filter(robjects=None).delete()
        return super().get(request, *args, **kwargs)

    def form_valid(self, form):
        form.save()
        robject = Robject.objects.last()
        Name.objects.filter(robjects=None).delete()
        RobjectFile.objects.filter(robjects=None).delete()
        return redirect("robjects_list", self.args[0])

    def get_initial(self):
        return {
            "create_by": self.request.user,
            "project": Project.objects.get(name=self.args[0])
        }

    def get_form_class(self):
        form = modelform_factory(Robject, form=ModelForm, exclude=["modify_by"],
                                 widgets={
            'names': AddAnotherWidgetWrapper(
                forms.SelectMultiple,
                reverse_lazy('add_name', args=[self.args[0]]),
            ),

            'files': AddAnotherWidgetWrapper(
                forms.SelectMultiple,
                reverse_lazy('add_file', args=[self.args[0]]),
            ),

            'tags': AddAnotherWidgetWrapper(
                forms.SelectMultiple,
                reverse_lazy('add_tag', args=[self.args[0]]),
            ),
            'project': forms.HiddenInput(),
            'create_by': forms.HiddenInput()
        })
        return form

    def get_permission_object(self):
        project = Project.objects.get(name=self.args[0])
        return project

class CreateNameView(LoginRequiredMixin, CreatePopupMixin, CreateView):
    model = Name
    fields = "__all__"
    template_name = 'robjects/create_name.html'

    def get_initial(self):
        return {"project": Project.objects.get(name=self.args[0])}

    def get_form(self):
        form = super(CreateNameView, self).get_form()
        form.fields['project'].widget = forms.HiddenInput()
        return form

class RobjectFileCreateView(LoginRequiredMixin, CreatePopupMixin, CreateView):
    model = RobjectFile
    fields = ['_file', 'description']
    template_name = 'robjects/create_robject_file.html'


class TagCreateView(LoginRequiredMixin, CreatePopupMixin, CreateView):
    model = Tag
    fields = ['name', "project"]
    template_name = 'robjects/create_tag.html'

    def get_initial(self):
        return {"project": Project.objects.get(name=self.args[0])}

    def get_form(self):
        form = super(TagCreateView, self).get_form()
        form.fields['project'].widget = forms.HiddenInput()
        return form

提前致谢。

2 个答案:

答案 0 :(得分:1)

点击按钮后,您必须切换到弹出窗口,无论天气如何,您的流行音乐会自动关闭或不需要切换回来。请尝试以下

        //Before clicking on the button which opens pop up, capture the parent window handle.
                    parent = driver.current_window_handle

        //Click on button

                            time.sleep(1)

       //Switch to the latest window

                        for winhandle in driver.window_handles:
                            print "Handle = ",handle
                            driver.switch_to_window(handle);
                            print driver.title

      //Click on submit and wait & switch back to parent window

                        time.sleep(2)
                        driver.switch_to_window(parent);

答案 1 :(得分:0)

问题出在浏览器驱动程序中。我通过从Firefox驱动程序切换到Chrome驱动程序来解决它。