Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

python - "NoSuchWindowException: no such window: window was already closed" while switching tabs using Selenium and WebDriver through Python3

I have a form that opens in a new tab when I click on it. When I try to navigate to that new tab, I keep getting a NoSuchWindowException. Code is pretty straightforward. 'myframe' is the frame within the new tab that the information will eventually get plugged into. Should I be waiting for something else?

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
import pandas as pd

url = *****
driver = webdriver.Chrome(executable_path = r'S:EngineeringJakeMasterControl Completing Pipette CalPM Formschromedriver')
driver.get(url)

wait = WebDriverWait(driver, 5)    

window_before = driver.window_handles[0]
driver.find_element_by_id('portal.scheduling.prepopulate').click()
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
driver.switch_to_default_content()
wait.until(EC.frame_to_be_available_and_switch_to_it('myframe'))

Traceback (most recent call last):

  File "<ipython-input-308-2aa72eeedd51>", line 1, in <module>
    runfile('S:/Engineering/Jake/MasterControl Completing Pipette CalPM Forms/Pipette Completing CalPM Tasks.py', wdir='S:/Engineering/Jake/MasterControl Completing Pipette CalPM Forms')

  File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 678, in runfile
    execfile(filename, namespace)

  File "C:ProgramDataAnaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 106, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "S:/Engineering/Jake/MasterControl Completing Pipette CalPM Forms/Pipette Completing CalPM Tasks.py", line 150, in <module>
    create_new_cal_task(asset_number)

  File "S:/Engineering/Jake/MasterControl Completing Pipette CalPM Forms/Pipette Completing CalPM Tasks.py", line 130, in create_new_cal_task
    driver.switch_to_default_content()

  File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emotewebdriver.py", line 783, in switch_to_default_content
    self._switch_to.default_content()

  File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emoteswitch_to.py", line 65, in default_content
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})

  File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emotewebdriver.py", line 314, in execute
    self.error_handler.check_response(response)

  File "C:ProgramDataAnaconda3libsite-packagesseleniumwebdriver
emoteerrorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

NoSuchWindowException: no such window: window was already closed
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 6.1.7601 SP1 x86_64)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per your question and your code trials when you intend to navigate to a new tab you need to:

  • Induce WebDriverWait while switching to the New Tab.
  • Again when you intend to switch() to the desired <iframe> you need to induce WebDriverWait again.
  • While you switch() to the desired <iframe> try to use either ID, NAME, XPATH or CSS-SELECTOR of the desired <iframe>.
  • Your own code with these simple modification will be as follows:

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    
    url = "your_url"
    driver = webdriver.Chrome(executable_path = r'S:EngineeringJakeMasterControl Completing Pipette CalPM Formschromedriver')
    driver.get(url)
    windows_before  = driver.current_window_handle
    driver.find_element_by_id('portal.scheduling.prepopulate').click()
    WebDriverWait(driver, 5).until(EC.number_of_windows_to_be(2))
    windows_after = driver.window_handles
    new_window = [x for x in windows_after if x != windows_before][0]
    # driver.switch_to_window(new_window) <!---deprecated>
    driver.switch_to.window(new_window)
    WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"myframe"))) # or By.NAME, By.XPATH, By.CSS_SELECTOR
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...