問題描述
如何使用 selenium (java) 在 IE 中打開一個新選項卡并在該選項卡(不是窗口)中打開一個 url?我正在使用以下代碼打開一個新標簽頁?
How do I open a new tab in IE using selenium (java) and open a url in that tab (not window)? I am using the below code to open a new tab?
driver.get("https://google.com/");
//below line of code opens a new tab but does sets control on new tab.
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");//opens new tab
// As control does not sets on new tab, the below link opens on first tab only..
driver.get("https://facebook.com/");//but load facebook in first tab i.e on google page
誰能告訴我,如何將控制權轉移到新標簽頁,以便 facebook 鏈接在該新標簽頁中打開.
Can anyone tell me, how to shift control to new tab so that the facebook link opens in that new tab.
你好
我使用 Selenium Web-Driver 版本 2.40 和 IE 11.0
I using Selenium Web-Driver Version 2.40 and IE 11.0
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.manage().window().maximize();
driver.get("https://google.com/");
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");//opens new tab
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window //Switch to new window open
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
driver.get("https://facebook.com/");
}
// Perform the actions on new window
//Close the new window, if that window no more required
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
我無法在同一窗口的新標簽頁上打開 facebook..
I am not able to open facebook on new tab of same window..
問候沙申克·戈亞爾
推薦答案
你需要使用
driver.switchTo().window(String)
像打開新窗口一樣切換到出現的窗口.
to switch to the window that has come up just as you would with opening a new window.
這篇關于如何使用 selenium(java)在 IE 中打開新選項卡并在該選項卡(不是窗口)中打開一個 url的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!