1

I am trying to automate credential validation using Python/Selenium. The target web-page is in the below mentioned code block.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


b = webdriver.Chrome('chromedriver.exe')
b.get('http://220.156.189.33/esamudraUI/jsp/examination/checker/COCSearch.jsp?hidProcessId=COC')
parent = b.current_window_handle

# Clicking on the Calendar icon to enter date:
b.find_element_by_xpath(
    '/html/body/form/table[4]/tbody/tr/td/table/tbody/tr[2]/td[3]/table/tbody/tr[5]/td[2]/span/a/font/b/img').click()

# Switching to the pop-up calendar window:
child = b.window_handles[1]      
b.switch_to_window(child)

# Selecting month and year from drop-down:
month = Select(b.find_element_by_name('month'))
month.select_by_value('11')
year = Select(b.find_element_by_name('year'))
year.select_by_value('1986')

# Clicking on the required day in the table:
table = b.find_element_by_xpath('/html/body/form/table/tbody/tr[2]/td/table')
tags = table.find_elements_by_tag_name("td")
for tag in tags:
    if tag.text == '3':
        tag.click()

However, as soon as the code completes execution, NoSuchWindowException is thrown.

---------------------------------------------------------------------------
NoSuchWindowException                     Traceback (most recent call last)
<ipython-input-184-94324ee00e42> in <module>
     19     if tag.text == '3':
     20         tag.click()
---> 21         b.close()
     22 #         while True:
     23 #             try:

~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in close(self)
    686             driver.close()
    687         """
--> 688         self.execute(Command.CLOSE)
    689 
    690     def quit(self):

~\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=83.0.4103.116)

I tried using WebDriverWait(b, 5).until(EC.number_of_windows_to_be(1)) after tag.click() in the last line of the code, but the Exception remains. I checked the class-method click() under WebElement class under Selenium web-driver but I got no clue on how to remove this exception:

def click(self):
        """Clicks the element."""
        self._execute(Command.CLICK_ELEMENT)

Kindly give some advice.

DRaj
  • 11
  • 2
  • you need to switch the driver back to the remaining window. Btw, switching by index is a little dodgy as the order of the array is not guaranteed. – pcalkins Jul 01 '20 at 17:57
  • I tried the following: `for tag in tags: if tag.text == '3': tag.click() b.switch_to_window(parent)` But I got "StaleElementReferenceException". – DRaj Jul 01 '20 at 18:04
  • Seems like the click is occurring here, it's when you do b.close(); that you get the Exception. (Because the window has already closed) At that point the driver is in nowhere land. You need to switch it to an open window. – pcalkins Jul 01 '20 at 19:40
  • @pcalkins But the `parent` window is already open, I had only switched over to `child` window. As soon as it closed, `parent` became default, and it appeared with the **date entry** at its place. Yet the `NoSuchWindowException` is thrown. I also tried `WebDriverWait` after the `tag.click()` but it does not prevent the *Exception* – DRaj Jul 02 '20 at 03:51
  • @DebanjanB I tried the steps you had mentioned in the answers of similar questions i.e. `WebDriverWait` before switching to `child` window, and again after `tag.click()` in the `child` window. But they don't prevent the *Exception*. – DRaj Jul 02 '20 at 03:57
  • parent does not become default for the driver, you have to switch it back. – pcalkins Jul 02 '20 at 16:33
  • @pcalkins `for tag in tags: if tag.text == '3': tag.click() b.switch_to_window(parent)` This throws `StaleElementReferenceException`. Switching to `parent` is just after the `click()` in the same `for` loop. – DRaj Jul 02 '20 at 18:49
  • get the window handles again... – pcalkins Jul 03 '20 at 16:00
  • @pcalkins Can you kindly try my code and find the bug. Can you also try what you are suggesting me to do and see if it works. I have tried getting the window handles again but the mere click on the calendar date closes the date window and throws exception. – DRaj Jul 04 '20 at 16:41
  • your exception appears to be coming from " b.close()". That's a command to close the current window. Since the driver is in no-mans land the exception is thrown. You need to get the current window handles before that and switch to one that exists. – pcalkins Jul 07 '20 at 18:10
  • 1
    @pcalkins I resolved the issue by breaking the `for` loop after `tag.click`. Thanks – DRaj Jul 14 '20 at 08:35

0 Answers0