2014-09-21 84 views
0

有人可以帮我解决下面的代码有什么问题。我的意图是能够在浏览器中打开一个新标签。该脚本通过,但并没有真正打开一个新标签使用Ruby selenium打开浏览器选项卡

require 'selenium-webdriver' 

@browser = Selenium::WebDriver.for :chrome 
@browser.navigate.to "http://www.google.com" 
body = @browser.find_element(:tag_name => 'body') 
body.send_keys(:control, 't') 

p "total number of windows" 
p @browser.window_handles.length 
p "printing window ids" 
@browser.window_handles.each do |window| 
    p window 
end 
@browser.quit 
+0

你有没有找到任何有用的答案? – daremkd 2014-10-13 23:22:32

+0

其实以下答案都没有打开一个新标签。对不起,延迟回复。 – machzqcq 2014-11-24 01:57:05

+0

因此,在阅读关于打开新选项卡与新窗口(跨越所有浏览器)的更多信息之后,似乎更像是用户首选项设置。所以如果我想打开一个新标签,我应该创建一个,它没有指定width,height属性。如果指定了这些属性,单击该链接将打开一个新窗口 - http://stackoverflow.com/questions/726761/javascript-open-in-a-new-window-not-tab。似乎太多的工作只是为了打开一个新标签,并没有就如何通过脚本打开新标签与窗口达成共识。 – machzqcq 2014-11-24 20:16:55

回答

2

最近我不得不打开和管理使用Chrome新标签是:

require 'selenium-webdriver' 

driver = Selenium::WebDriver.for :chrome 
driver.get 'http://www.google.com' 

#Extract the link we want to go to 
address = driver.find_element(:link_text, "Gmail").attribute('href') 

#Open a new browser window 
driver.execute_script("window.open()") 

#Use the newest window 
driver.switch_to.window(driver.window_handles.last) 
driver.get 'http://yahoo.com' 
+0

谢谢。这有助于,但我也可以创建一个新的webdriver实例并使用它。我真的想做ctrl + t的确切操作,它通过代码在Chrome浏览器中打开一个新选项卡。还有什么想法? – machzqcq 2014-09-28 23:14:25

+0

这是一个Selenium问题,它不支持打开新标签,请参阅http://stackoverflow.com/questions/14550360/selenium-ide-for-firefox-ctrl-tab/14552367#14552367 – daremkd 2014-09-28 23:22:03

0

如果你在一个苹果,尝试:

body.send_keys(:command, 't') 

代替

body.send_keys(:control, 't') 

也看这里:How to open a new tab using Selenium WebDriver with Java?

+0

我已经看过这个链接你分享,这是当我发布这个问题,因为行为没有看到打开一个新的标签。我在windows btw上。还有什么想法? – machzqcq 2014-09-28 23:15:26

相关问题