How to open new window with Selenium?

1 Answers
Answered by suresh

To open a new window with Selenium, you can use the `window_handles` attribute to switch to the newly opened window after first opening it using the `driver.switch_to.window()` method. Below is a simple code snippet in Python using Selenium:

```html




How to Open New Window with Selenium - Interview Question Answer


How to Open New Window with Selenium - Interview Question Answer

If you need to open a new window with Selenium, you can achieve this by using the window_handles attribute and the switch_to.window() method. Here's a basic Python code snippet:

    from selenium import webdriver

    driver = webdriver.Chrome()
    driver.get("https://www.example.com")

    # Open a new window
    driver.execute_script("window.open('https://www.newwebsite.com', 'new_window')")

    # Switch to the new window
    new_window = driver.window_handles[1]
    driver.switch_to.window(new_window)
  



```

In this code, we first open a new window using `window.open()` method and then switch to that new window using the `driver.switch_to.window()` method along with the index of the newly opened window (in this case, index 1).

By following this approach, you can effectively handle opening new windows with Selenium in your automated testing scripts.

Answer for Question: How to open new window with Selenium?