gpt4 book ai didi

python - 通过 Python3 使用 Selenium 和 WebDriver 切换选项卡时出现 "NoSuchWindowException: no such window: window was already closed"

转载 作者:行者123 更新时间:2023-12-01 09:09:30 25 4
gpt4 key购买 nike

我有一个表单,当我单击它时会在新选项卡中打开。当我尝试导航到该新选项卡时,我不断收到 NoSuchWindowException。代码非常简单。 “myframe”是新选项卡中的框架,信息最终将插入其中。我应该等待其他事情吗?

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:\Engineering\Jake\MasterControl Completing Pipette CalPM Forms\chromedriver')
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:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.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:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 783, in switch_to_default_content
self._switch_to.default_content()

File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 65, in default_content
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})

File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)

File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.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)

最佳答案

根据您的问题和代码试验,当您打算导航到新选项卡时,您需要:

  • 切换新选项卡时引发WebDriverWait
  • 当您打算时再次 switch()到所需的<iframe>您需要再次引发WebDriverWait
  • 当你 switch()到所需的<iframe>尝试使用所需 <iframe>IDNAMEXPATHCSS-SELECTOR .
  • 经过这些简单修改,您自己的代码将如下所示:

    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:\Engineering\Jake\MasterControl Completing Pipette CalPM Forms\chromedriver')
    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

关于python - 通过 Python3 使用 Selenium 和 WebDriver 切换选项卡时出现 "NoSuchWindowException: no such window: window was already closed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com