gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:23 34 4
gpt4 key购买 nike

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

我希望能够在主窗口中使用 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

提前致谢。

最佳答案

单击按钮后,您必须切换到弹出窗口,无论弹出窗口是否自动关闭,您都需要切换回来。请尝试以下

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

关于python - Selenium:用户关闭弹出窗口后切换到主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45883123/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com