2013-10-03 89 views
2

我需要在新Chrome窗口的网页上​​打开链接。已经有一个question 但这似乎是RC。我试过 driver.getUserWindow().open("http....."); 但它不工作。有没有办法强制Chrome为所有链接做到这一点?理想情况下,我想知道如何强制驱动程序在新窗口中打开链接。 (我使用Java和操作系统Windows 7如何强制Selenium在新窗口中打开链接?

回答

1

我不知道你用什么语言/ OS,但这里是你如何在新窗口打开链接OS X上使用Ruby和webdriver的:

link = driver.find_element(:tag_name => 'a') 
action = driver.action 
key = :command # replace with :control on Win/Linux 
action.key_down(key) 
action.click(link) 
action.key_up(key) 
action.perform 

这将打开新的标签页的链接。如果需要新的窗口,你应该使用:shift关键。

您还可以覆盖click方法元素,所以它总是打开新窗口的链接。

6

您可以使用Actions类来执行此操作。

Actions act = new Actions(driver); 
WebElement onElement = Your element on which action has to be performed; 
act.contextClick(onElement).perform(); 
act.sendKeys("w").perform(); // If you want the link to open in new tab then use T instead of w 

希望这会有所帮助。快乐的编码。

相关问题