I have webpage which open new browser window on click. I am able to get 2 handles however driver.close() always closes the first/main window.
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("file:///D:/blackhole/print.html")
han = driver.window_handles
print("handles:", han) # gets 1 handle
time.sleep(2)
click_btn = driver.find_element_by_link_text('Print')
click_btn.click()
han = driver.window_handles
print("handles:", han) # gets 2 handles
driver.switch_to_window = han[1] # first element is always first window handle
driver.close() # main window close
Below webpage code which invokes new window
<a href="print.html"
onclick="window.open('popprint.html',
'newwindow',
'width=300,height=250');
return false;"
>Print</a>
Same behaviour for Firefox as well. Python 3.6.7
testShouldThrowNoSuchWindowExceptionOnAnyElementOperationIfAWindowIsClosed
in python bindings of Selenium. The test is passing. Please have a look at github.com/SeleniumHQ/selenium/blob/… Therefore, I feel thatdriver.close()
is working fine. – Unipodswitch_to_window
versusswitch_to.window()
. – Headreach