Switch tabs using Selenium WebDriver with Java

前端 未结 21 1465
野性不改
野性不改 2020-11-22 12:09

Using Selenium WebDriver with JAVA. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I

21条回答
  •  没有蜡笔的小新
    2020-11-22 12:57

    To get parent window handles.

    String parentHandle = driverObj.getWindowHandle();
    public String switchTab(String parentHandle){
        String currentHandle ="";
        Set win  = ts.getDriver().getWindowHandles();   
    
        Iterator it =  win.iterator();
        if(win.size() > 1){
            while(it.hasNext()){
                String handle = it.next();
                if (!handle.equalsIgnoreCase(parentHandle)){
                    ts.getDriver().switchTo().window(handle);
                    currentHandle = handle;
                }
            }
        }
        else{
            System.out.println("Unable to switch");
        }
        return currentHandle;
    }
    

提交回复
热议问题