Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Regain focus of parent of the frame that has been removed. #365

piotrj opened this Issue Jul 26, 2012 · 14 comments


None yet

piotrj commented Jul 26, 2012

I have a page that has an iframe but after some operations it closes that iframe. This causes the following error:

  Already at parent frame.

I guess this is because:

within_frame "frame1" do
  click_link "A"
  click_link "B" # At this point the iframe closes
end # so it's detached here and it raises and error

Maybe this could be fixed by remembering the parent frame at the moment of focusing on the child iframe and then at the end of the block:

def within_frame(frame_id_or_index)
  # parent = get the current frame
    browser.frame_focus(parent) # Regain the focus of the parent even if the frame has been detached

Does this make sense? Or maybe the situation I described is not supported on purpose?

eirc commented Aug 2, 2012

+1 on this one, my guess on what should (or I would like) to be done there is:

  • Remember currently focused frame
  • Focus target frame
  • Ensure that focus returns to previously remembered one
    • but don't raise exception is already there!

davidsf commented Oct 25, 2012


As a workaround, how can I get the current frame ? I can't find how

@davidsf Can you use the current URL instead?

iRonin commented Jan 16, 2013

I have the same issue.

Does anyone know how to get window's current_url after iframe has loaded content? For me the current_url points to iframe's url, not window's.

I'm having the same issue as iRonin - after navigating to a page with an embedded iframe, the current_url is the url of the iframe rather than the enclosing page.

Is there a solution for this?


mhoran commented Mar 10, 2013

#current_url is scoped to the frame, not to the window. You can verify the current_url of the window outside of within_frame, but once inside a within_frame block current_url will return the URL of the current frame.

Note that if the requested URL (before redirect) of the window and the iframe are the same, current_url for the window will return the iframe URL.

Regarding the original issue, we could back out the change which raises an error when a user tries to switch to the main frame. Ideally we'd just have the same behavior as Selenium.

+1 we are hitting sporadic "Already at parent frame" exceptions. Seems inconsistent and difficult to reproduce unfortunately. For example, when running the test by itself it passes, when running the full suite (slower I assume) it fails.

Ended up just removing within_frame and using more specific css selectors. Seemed to work.

+1 I can reproduce this consistently by clicking a link inside an iframe that closes the frame.

I can workaround this by using page.driver.browser.frame_focus(frame_id_or_index) and then letting the link return my frame to the parent.

Can anyone confirm that this is still an issue using the latest version of capybara-webkit?

I'll close this issue due to inactivity. Please re-open it if you're still experiencing these issues.

kolen commented Jun 24, 2016

I still have this problem in 1.11.1

let!(:iframe) { find 'body > div > iframe' }

it '...' do
  within_frame iframe do
    click_link 'X'
     Failure/Error: within_frame iframe do
       Already at parent frame.

This link 'X' inside iframe triggers removal of iframe from DOM.

@jferris jferris reopened this Jun 27, 2016


twalpole commented Jun 27, 2016

FYI: There is a spec for this behavior in Capybara master, and is the reason for the Capybara master failure in the last passing travis build of capybara-webkit - https://travis-ci.org/thoughtbot/capybara-webkit/jobs/127799105

@twalpole twalpole referenced this issue in teamcapybara/capybara Jul 14, 2016


Capybara 2.8.0 release #1721

snelson commented Oct 28, 2016

We're still having this problem as well in 1.11.1. Here's a temporary workaround we've been using that avoids the page.driver.browser deprecation warnings:

  def within_frame(selector)
    page.within_frame(selector) do
  rescue Capybara::Webkit::InvalidResponseError => e
    raise(e) unless e.message == "Already at parent frame."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment