How to access to the new page with form and target="_blank" through capybara

540 Views Asked by At

I am trying to scrape this page with capybara + poltergeist.

<form action="/agency/advertiser/login" method="POST" role="form" target="_blank">    
    <input type="hidden" name="csrfToken" value="token"/>
    <input type="hidden" name="account_id" value="id">
    <input type="submit" value="login" class="btn btn-warning">                      
</form>

I could access to the element below, and tried click.

    <input type="submit" value="login" class="btn btn-warning">                      

However, I can't access to the new page that opens after the click. How can I do that?

1

There are 1 best solutions below

0
Thomas Walpole On

You need to tell Capybara you want to work in the new window. You do that by getting a handle to newly opened window and then using within_window or switch_window.

new_window = page.window_opened_by do
  # perform whatever action causes the new window to open
  page.find('form').click_button
end

page.within_window(new_window) do
  # perform actions in the new window
end

# context returned to the original window
...