0

Am currently using windows handling for opening the map direction in the new window and after it opens i will be closing the child window, which is opened and do the remaming work in the code.But it is closing the whole browser, while debugging it is working correctly , but while running the code, am Getting the error as,

Error - selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed

Down i have attached the code,

   ##Clicking on The Map Image
            self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
            ##Setting up an Window Handle to get the size.
            handels =self.driver.window_handles
            size = len(handels)
            """
            The Below For Loop, We are using For Handling The Mutilple Windows,
            Which are opened in the Browser.
            """

            for length in range(size):
                driver.switch_to.window(handels[length])
                print(self.driver.title)
                time.sleep(3)
                if length == 1:
                    driver.close()

Where i have done the error i dunno. please sort me out.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
koushick
  • 497
  • 2
  • 8
  • 30
  • So you're clicking something that opens new window and you want to close main window and switch to new window to continue your actions, right? – Andersson Dec 17 '18 at 14:57
  • you are Right, am Clicking on Something that will open in new window and i will close that window which i was clicked, and contuine my actions, i Hope you got that! @Andersson – koushick Dec 17 '18 at 15:00
  • Post the URL that you are using and the manual scenario that opens a new window. – JeffC Dec 17 '18 at 16:01

2 Answers2

1

Try this code to close new window and switch back to main window

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Get current window
current = self.driver.current_window_handle
# Get current windows
handles = self.driver.window_handles
# Click button. Consider to use more reliable relative XPath instead of this absolute XPath
self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
# Wait for new window
WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(handles))
# Switch to new window
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
# Close new window
self.driver.close()
# Switch back to main window
self.driver.switch_to.window(current)
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • it is closing the my parent Window, not closing the window i clicked @Andersson – koushick Dec 17 '18 at 15:11
  • 1
    @koushik , OK... So I might misunderstood you... How many windows you need to handle? What is that *parent* and what is *the window you clicked*? These are not the same windows? – Andersson Dec 17 '18 at 15:12
  • Let Me Tell you the Secnario, I Will be going to the Locations and will be clicking on the one Map image which will open in new window, i need to close that one only . this "self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()" will open in new window above i mentioned in code. – koushick Dec 17 '18 at 15:15
  • i Hope now you got right? any clarifications needed? – koushick Dec 17 '18 at 15:18
  • @koushick , hmm... so you want to close new window which opens after you made a click and get back to main window? – Andersson Dec 17 '18 at 15:20
1

In short the switching to the new window handle wasn't clean.

Solution

  • Always keep track of the Parent Window handle so you can traverse back later if required as per your usecase.
  • Always use WebDriverWait with expected_conditions as number_of_windows_to_be(num_windows) before switching between Tabs/Windows.
  • Always keep track of the Child Window handles so you can traverse whenever required.
  • Always use WebDriverWait with expected_conditions as title_contains("partial_page_title") before extracting the Page Title.
  • Here is your own code with some minor tweaks mentioned above:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    handels_initially  = driver.window_handles
    self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()
    WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
    handels_now =self.driver.window_handles
    new_handle = [x for x in handels_now if x != handels_initially][0]
    driver.switch_to.window(new_handle)
    WebDriverWait(driver, 20).until(EC.title_contains("partial_title"))
    print(self.driver.title)
    driver.close()
    
  • You can find a detailed discussion in Selenium Switch Tabs

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Nice Elegant Way to code,More to learn, Thanks! This Also Working. But one Doubt, Why Can't we loop throw the windows, like in my above code.? – koushick Dec 18 '18 at 05:38
  • 1
    @koushick You can't loop throw the windows like in your code as `handels =self.driver.window_handles` doesn't ensures that the items in the _List_ will be stored in a _first generated first inserted_ basis and the _List_ can be random. – undetected Selenium Dec 18 '18 at 06:01
  • 1
    Fine, now i got that, it will be not excatly identifies the List which is being stored. Thanks @DebanjanB if you found my question useful, upvote it so others might know – koushick Dec 18 '18 at 06:03