How to switch to the new browser window, which opens after click on the button?
To switch to a new browser window in Selenium, you can use the switchTo()
method with the WindowHandle
of the new window. Here is an example of how you can do this in Java:
// Click on the button that opens the new window
driver.findElement(By.id("button")).click();
// Get the handle of the current window
String currentWindowHandle = driver.getWindowHandle();
// Get the handles of all open windows
Set<String> windowHandles = driver.getWindowHandles();
// Remove the handle of the current window from the set of handles
windowHandles.remove(currentWindowHandle);
// Switch to the new window
driver.switchTo().window(windowHandles.iterator().next());
This code clicks on a button with the ID "button", which opens a new window. It then gets the handle of the current window and stores it in a variable. It then gets the handles of all open windows and removes the handle of the current window from the set. Finally, it switches to the new window by using the switchTo()
method with the remaining handle in the set.
Note that this code assumes that there is only one other window open besides the current window. If there are multiple windows open, you will need to iterate through the set of handles and choose the appropriate one.