Mathew Kirmuć Mathew Kirmuć - 1 year ago 68
Python Question

Selenium: switching to main window after popup window closed by user

I have an app where the user can click button and popup window shows up. Then, after clicking submit button in this popup window, the window gets closed and the user continues work in the main window.

I want to be able to select elements using selenium in the main window, then select elements in the popup and after popup get closed, again in the main window. Any ideas how would I do this?


Here is my code. I'm using django-addanother to create form with popups. Main form is Robject form. Rest forms are popups.

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',

def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

def form_valid(self, form):
robject = Robject.objects.last()
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"],
'names': AddAnotherWidgetWrapper(
reverse_lazy('add_name', args=[self.args[0]]),

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

'tags': AddAnotherWidgetWrapper(
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

Thanks in advance.

Answer Source

The problem was in browser driver. I solved it by switching from Firefox driver to Chrome driver.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download