Search code examples
pythonpython-2.7selenium-webdriverwindow-handles

window handling in selenium (python)


My simple script written in python (selenium) need to click on number of hyperlinks (around 25) in a single web page, each time need to assert something in a new window opened.

I am using the following function to navigate between the windows, which also works well.

def go_window(self, window_name = None):
    if window_name is None:
        self.driver.switch_to_window(self.window_handle)
    else:
        self.driver.switch_to_window(window_name)

However, it does not close the new window opened each time (also because, links in my page open up the new page each time it is clicked :( ).

I would want to close the new window after asserting. Any help would be appreciated. Thanks.


Solution

  • Use WebDriver.close method, which closes the current window:

    self.driver.close()