如何通过capybara访问带有form和target =“ _ blank”的新页面

时间:2019-03-12 14:57:03

标签: ruby-on-rails capybara poltergeist

我正试图与水豚+ 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>

我可以访问下面的元素,并尝试了click

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

但是,我无法访问单击后打开的新页面。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要告诉Capybara您想在新窗口中工作。为此,您需要获取新打开的窗口的句柄,然后使用within_windowswitch_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
...