Switch tabs using Selenium WebDriver with Java

前端 未结 21 1144
野性不改
野性不改 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 13:05

    The flaw with the selected answer is that it unnecessarily assumes order in webDriver.getWindowHandles(). The getWindowHandles() method returns a Set, which does not guarantee order.

    I used the following code to change tabs, which does not assume any ordering.

    String currentTabHandle = driver.getWindowHandle();
    String newTabHandle = driver.getWindowHandles()
           .stream()
           .filter(handle -> !handle.equals(currentTabHandle ))
           .findFirst()
           .get();
    driver.switchTo().window(newTabHandle);
    

提交回复
热议问题