Switching to a Newly Opened Browser Window After a Button Click
When a button on a web page is clicked, it's possible for a new browser window to open and display search results. To interact with the new window, you can use Selenium WebDriver, a web automation framework.
To switch between browser windows, you can store the handle of the current window before clicking the button, then iterate through the available window handles and switch to the new window. Once you've completed your actions in the new window, you can close it and switch back to the original window.
Here's an example of how you might achieve this:
// Store the current window handle String winHandleBefore = driver.getWindowHandle(); // Perform the click operation that opens new window // Switch to new window opened for(String winHandle : driver.getWindowHandles()){ driver.switchTo().window(winHandle); } // Perform the actions on new window // Close the new window, if that window no more required driver.close(); // Switch back to original browser (first window) driver.switchTo().window(winHandleBefore); // Continue with original browser (first window)
By following this approach, you can effectively interact with multiple browser windows and return to the original window once your tasks have been completed.
The above is the detailed content of How to Switch Between Browser Windows Using Selenium WebDriver After a Button Click?. For more information, please follow other related articles on the PHP Chinese website!