如何使用Selenium webdriver在Capybara中打开一个新窗口?

时间:2012-10-24 19:59:06

标签: ruby selenium capybara

我找到了一个代码:page.driver.browser.switch_to.window,显然已切换到已打开的窗口。我们如何使用page.driver.browser对象打开一个新窗口?

谢谢

2 个答案:

答案 0 :(得分:2)

我最终使用JavaScript来执行此操作:

When /^I open a new window for "([^"]*)"$/ do |url|
str =
<<END_TAG
     window.open("#{url}", "window_name", "height=800,width=1000");
END_TAG
  page.execute_script(str)
end

答案 1 :(得分:0)

我最终在这里,即使是一个旧线程,可能对某人有用,你可以创建和使用这样的窗口:

new_window = open_new_window
switch_to_window new_window

或:

within_window new_window do
end

你也可以通过windows对象找到新窗口。

open_new_window
new_window = windows.last

参考文献:

http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Session:open_new_window

http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FSession%3Aswitch_to_window

https://github.com/teamcapybara/capybara#working-with-windows

With Capybara, how do I switch to the new window for links with "_blank" targets?