2013-10-03 87 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 

希望這會有所幫助。快樂的編碼。

相關問題