How to switch between two windows in browser using Selenium java

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

    Yes this is possible. First you need to save the refrence to current window.

    String parentWindow= driver.getWindowHandle();
    

    The after having clicked the link, you need to switch to the other window.

    List allWindows = driver.getWindowHandles();
    for(String curWindow : allWindows){
        driver.switchTo().window(curWindow);
    }
    

    This is where you perform operations on new window, finally closing it with

    driver.close();
    

    and switch back to parent window

    driver.switchTo().window(parentWindow);
    

提交回复
热议问题