目前chrome是我在實現webdriver時運行最穩定的浏覽器,如何利用webdriver打開多個標簽頁和鏈接呢,到處查找得到的往往只是如何打開標簽頁。
打開標簽頁很簡單,chrome浏覽器打開標簽頁的快捷鍵是ctrl+t,那把ctrl+t的按鍵事件傳入即可,很多種實現方式,以下只列出兩種:
1:
Actions actionOpenLinkInNewTab = new Actions(driver);
actionOpenLinkInNewTab.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform();
2:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
在新標簽頁打開新的鏈接全部代碼(Google到的):
String baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.get("https://www.facebook.com");
driver.switchTo().window(tabs.get(0)); // switch back to main screen
driver.get("https://www.news.google.com");
url:http://equalxx.iteye.com/blog/2291520