How to switch between two windows in browser using Selenium java

前端 未结 8 1176
面向向阳花
面向向阳花 2020-12-11 06:57

I\'m working with Selenium Automation. In this, When i click a link in a current window, a new window opens. I just want to switch the control to the new window. But i can\'

8条回答
  •  粉色の甜心
    2020-12-11 07:33

    Here is the best methods to switch in window using index and title. you can add in base class and use it frequently.

    public  void switchToWindow(String windowTitle) {
        Set windows = driver.getWindowHandles();
        for (String window : windows) {
            driver.switchTo().window(window);
            if (driver.getTitle().contains(windowTitle)) {
                return;
            }
        }
    }
    
    
    
    
      public void switchToWindow(int index) {
        Set windows = driver.getWindowHandles();
        int totalWin= windows.size();
        String winTitle = null;
        for(int i=0;i

提交回复
热议问题