Selenium:在用户关闭弹出 window 后切换到 main window
Selenium: switching to main window after popup window closed by user
我有一个应用程序,用户可以在其中单击按钮并弹出 window。然后,单击此弹出窗口 window 中的提交按钮后,window 关闭,用户继续在主 window 中工作。
我希望能够 select 元素在主 window 中使用 selenium,然后 select 弹出窗口中的元素,在弹出窗口关闭后,再次在主 window。任何想法我将如何做到这一点?
编辑:
这是我的代码。我正在使用 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
提前致谢。
单击按钮后,您必须切换到弹出窗口,无论天气如何,您的弹出窗口是否会自动关闭,您都需要切换回来。试试下面
//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);
问题出在浏览器驱动程序中。我通过从 Firefox 驱动程序切换到 Chrome 驱动程序来解决它。
我有一个应用程序,用户可以在其中单击按钮并弹出 window。然后,单击此弹出窗口 window 中的提交按钮后,window 关闭,用户继续在主 window 中工作。
我希望能够 select 元素在主 window 中使用 selenium,然后 select 弹出窗口中的元素,在弹出窗口关闭后,再次在主 window。任何想法我将如何做到这一点?
编辑:
这是我的代码。我正在使用 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
提前致谢。
单击按钮后,您必须切换到弹出窗口,无论天气如何,您的弹出窗口是否会自动关闭,您都需要切换回来。试试下面
//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);
问题出在浏览器驱动程序中。我通过从 Firefox 驱动程序切换到 Chrome 驱动程序来解决它。