2017-09-13 56 views
2

我正在嘗試使用Ctrl +單擊鏈接在新選項卡中打開它。在Chrome 58.這是工作的罰款請看以下代碼:在Selenium Webdriver中使用Ctrl +單擊組合打開新選項卡

action.keyDown(Keys.CONTROL).click(driver.findElement(By.xpath 
("//section[@class='filmStrip__basic']//a[text()='En savoir 
plus']"))).keyUp(Keys.CONTROL).build().perform(); 

我使用IE,Firefox和Safari瀏覽器相同的代碼,但得到以下錯誤:

火狐54:鏈路越來越在同一個標​​籤中打開。 IE 11:沒有發生..控制移動到下一行的Safari :例外上action.keyDown-無法識別的命令

幫助地與任何一個瀏覽器也被理解。

感謝

回答

1

另一種方法是使用JavaScript執行:

JavascriptExecutor jse = (JavascriptExecutor) driver; 
jse.executeScript("window.open('','_blank');"); 

至於你的問題,我太並沒有發現任何有用的,直到我發現了這個解決辦法。 我甚至嘗試:solution with ctrl + enter

+0

我要測試的超鏈接的功能,以及。所以,需要實際點擊鏈接才能打開它。 –

+0

爲什麼不使用'click'? – Edwin

+0

點擊正在打開它在同一個標​​籤。我想一口氣測試整個流程。謝謝 –

2

當你試圖點擊一個鏈接,是一個<a>標籤內的,而不是xpath可以使用linkText定位。下面是示例代碼打開URL http://www.google.com,驗證Page Title,使用Actions類點擊Gmail鏈接在新標籤中打開https://accounts.google.com

String URL="http://www.google.com"; 
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); 
WebDriver driver = new FirefoxDriver(); 
driver.get(URL); 
System.out.println("Page Title is : "+driver.getTitle()); 
WebElement link = driver.findElement(By.linkText("Gmail")); 
Actions newTab = new Actions(driver); 
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform(); 
+0

它運行良好。 :) –

1

嘗試這樣....

// specify chromedriver.exe directory path and replace in "driverPath" 

      String driverPath = "C:/Users......"; 
      WebDriver driver; 
      System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe"); 
      driver = new ChromeDriver(); 

      System.out.println("lanuching 1st url in tab1"); 

      driver.navigate().to(
        "https://amazon.com"); 

      System.out.println("lanuched 1st url in tab1"); 
      Thread.sleep(30000); 
    ((JavascriptExecutor) driver).executeScript(
        "window.open('http://ebay.com');"); 
      Thread.sleep(20000); 
      Set<String> allwh = driver.getWindowHandles(); 
      System.out.println(allwh.size()); 
      for (String v : allwh) { 
       System.out.println(v); 
       driver.switchTo().window(v); 
       String title = driver.getTitle(); 
       System.out.println("2nd url in tab2" + title); 
相關問題