【发布时间】:2014-05-14 06:31:36
【问题描述】:
如何使用 selenium (java) 在 IE 中打开一个新选项卡并在该选项卡(不是窗口)中打开一个 url? 我正在使用以下代码打开一个新标签页?
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 链接。
你好
我使用 Selenium Web-Driver 版本 2.40 和 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..
问候 沙申克·戈亚尔
【问题讨论】:
-
这个链接详细解释了如何切换窗口:stackoverflow.com/questions/9588827/…
-
我要求在新标签上工作,而不是在新窗口上工作。
-
Selenium 将标签视为新窗口。
-
注意:AFAIK,Selenium doesnot open new tabs, only new windows。我相信您的解决方法会起作用,但是 selenium 仍会将其视为新窗口,您可以使用
driver.switchTo().window(windowHandle)切换到它 -
我正在使用 selenium 2.40 和 IE 11.0,我无法在同一窗口的新选项卡中打开链接。我提供了上面的代码,我正在使用。你能提供代码吗java,这样我就可以解决这个问题。