2017-08-25 195 views
0

我有一個應用程序,用戶可以點擊按鈕並彈出窗口顯示。然後,在此彈出窗口中單擊提交按鈕後,窗口將關閉,用戶繼續在主窗口中工作。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 

在此先感謝。

+0

向我們顯示您的代碼, – Kapil

回答

0

問題出在瀏覽器驅動程序。我通過從Firefox驅動程序切換到Chrome驅動程序來解決此問題。

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); 
+0

問題是,當硒點擊提交按鈕並彈出關閉時,測試被暫停。這意味着他們不會破產,不會走得更遠。沒有錯誤,沒有反饋,什麼都沒有。 –

+0

https://stackoverflow.com/questions/45878383/selenium-django-tests-stops-when-testing-popup-window –