Multiwindow handling | Selenium Python Forum
N
Nitika Sharma Posted on 02/11/2020
 

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(10)
handles = set(driver.window_handles)
print(len(handles))
driver.get("https://www.naukri.com/")
handles = set(driver.window_handles)
print(len(handles))
print(driver.current_window_handle)
print("-----")
sets = iter(handles)
for i in sets:
print(i)
try:
driver.switch_to.window(i)
driver.close()
except Exception as e:
print(e)
# finally:
# driver.switch_to.default_content()

# #close - close the window under focus
# #quit - closes all the windows in current session
#
# driver.switch_to.window(secondPopUp)
# driver.close()
#
# driver.switch_to.window(mainwindow)
# time.sleep(5)
# driver.quit()

Hi, facing issue in multiwindow handling,

When I am executing final block to switch to default content then it is giving me error, because in for loop the default window is already closed, so please share the code to how to switch to the default content.


J
Jaspreet Replied on 04/11/2020

Hey,

Kindly modify the given code according to your try-except code.

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
currentWindow = driver.current_window_handle

driver.get("https://naukri.com/")
windowid = driver.window_handles
print(windowid)
print("")
idSet = iter(windowid)
for i in windowid:
    if(i!=currentWindow):
        print(i)
        driver.switch_to.window(i)
        driver.close()
    else:
        print("Default window ->"+str(currentWindow))
        driver.switch_to.default_content()
        print(driver.find_element_by_xpath("//div[text()='Login']").text)


time.sleep(5)
driver.quit()


N
Nitika Sharma Replied on 04/11/2020

 

okay mam, Thanku so much


J
Jaspreet Replied on 07/11/2020

Hey

I hope your issue got resolved.


Related Posts