How to switch between two windows in browser using Selenium java

前端 未结 8 1186
面向向阳花
面向向阳花 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:55

        // fetch all windows before clicking on new window link.
        Set windowHandles = driver.getWindowHandles();
        // Click on link to open new window
        driver.findElement(By.tagName("a")).click();  // link to open new window
    
        Set updatedWindowHandles = driver.getWindowHandles();
        updatedWindowHandles.removeAll(windowHandles);
        for (String window: updatedWindowHandles) {
            driver.switchTo().window(window);
        }
    

提交回复
热议问题