是的,只是为了证明这一点,我编写了以下代码:-
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://en.wikipedia.org/wiki/Cascading_Style_Sheets"
link_arr = driver.find_elements(:xpath,"//a")[2..4]
link_arr.map!{|e| e.attribute("href")}
link_arr.each do |link|
driver.execute_script("window.open(\"#{link}\")") if link
end
p driver.window_handles.size # => 4
# the below will give you the title of each opened window
p driver.window_handles.map{|d| driver.switch_to.window(d);driver.title}
# => ["Cascading Style Sheets - Wikipedia, the free encyclopedia",
#"Wiki Loves Monuments India | The Wikipedia photo contest around Cultural Heritage",
#"Wiki Loves Monuments India | The Wikipedia photo contest around Cultural Heritage",
#"Cascading Style Sheets - Wikipedia, the free encyclopedia"]
这样您就可以转到任何所需的浏览器窗口,完成您的工作并关闭窗口。