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

问题描述 投票:0回答:1

我试图用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>

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

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

但是,我无法访问点击后打开的新页面。我怎样才能做到这一点?

ruby-on-rails capybara poltergeist
1个回答
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
...
© www.soinside.com 2019 - 2024. All rights reserved.