1

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?

Makoto Taguchi
  • 129
  • 1
  • 8
  • 1
    Possible duplicate of [With Capybara, how do I switch to the new window for links with "\_blank" targets?](https://stackoverflow.com/questions/7612038/with-capybara-how-do-i-switch-to-the-new-window-for-links-with-blank-targets) – MrShemek Mar 12 '19 at 15:12

1 Answers1

1

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
...
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78