在Selenium中不再位于DOM之后切换回父帧 [英] Switching back to parent frame after it's no longer in DOM in Selenium

查看:199
本文介绍了在Selenium中不再位于DOM之后切换回父帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下页面

<body>
  <iframe id="outer_frame">
    <iframe id="search_button_frame">
      <button id="search_button"></button>
    </iframe>
  </iframe>
</body>

现在点击 search_button 后两个框架outer_frame search_button_frame 不再在DOM中,而是页面变得像这样

Now after I click on the search_button the two frames outer_frame and search_button_frame are no longer in DOM and instead the page becomes like this

<body>
  <iframe id="form_frame">
    ...
  </iframe>
</body>

我正在尝试去 search_button 然后将帧退出到主页面,然后使用以下方法切换到 form_frame

I'm trying to go to the search_button then get out of the frames into the main page, and then switching to the form_frame using this:

outer_frame = browser.find_element_by_xpath('//*[@id="outer_frame"]')
browser.switch_to.frame(outer_frame)

search_button_frame = browser.find_element_by_xpath('//*[@id="search_button_frame"]')
browser.switch_to.frame(search_button_frame)

search_button = browser.find_element_by_xpath('//*[@id="search_button"]')
search_button.click()

browser.switch_to_default_content()

form_frame = browser.find_element_by_xpath('//*[@id="form_frame"]')
browser.switch_to.frame(form_frame)

但是我一直收到以下错误:

but I keep getting the following error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Una
ble to locate element: {"method":"xpath","selector":"//*[@id="form_frame"]"}

这是否意味着我没有位于正确的框架中?

does this mean I'm not positioned in the right frame?

推荐答案

根据您的问题和 DOM树 结构点击 search_button 您需要先切换 default_content(),然后切换到下一个所需的< iframe> 使用 WebDriverWait ,如下所示:

As per your question and the DOM Tree structure post clicking on the search_button you need to switch to the default_content() first and then switch to the next desired <iframe> using WebDriverWait as follows:

driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"form_frame")))

您可以在以下网址找到详细讨论:

You can find a detailed discussion in:

  • How can I select a html element no matter what frame it is in in selenium?
  • How to properly wait for a frame to be available in python-selenium?

这篇关于在Selenium中不再位于DOM之后切换回父帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆