如何使用Selenium(Java)在IE中打开新选项卡并在该选项卡中打开URL(而非新窗口)

3
如何使用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。

敬礼, Shashank Goyal


这个链接详细解释了如何切换窗口:https://dev59.com/yWkw5IYBdhLWcg3w4eYD - Richard
我要求在新标签页上工作,而不是在新窗口中。 - user3004247
Selenium认为一个选项卡是一个新窗口。 - Richard
注意:据我所知,Selenium不会打开新标签页,只会打开新窗口。我相信您的解决方法是可行的,但Selenium仍然会将其视为新窗口,并且您可以使用 driver.switchTo().window(windowHandle) 切换到它。 - Faiz
我正在使用Selenium 2.40和IE 11.0,但无法在同一窗口的新标签中打开链接。我已经提供了我正在使用的代码。您能否请用Java提供代码,以便我可以解决这个问题。 - user3004247
2个回答

1

您需要使用

driver.switchTo().window(String)

要切换到弹出的窗口,就像打开新窗口一样。


有没有窗口和新标签页之间的区别?我在这里询问的是新标签页,而不是新窗口。 - user3004247
没有区别,不是的。它仍然被视为“窗口句柄”,因此它们并不是互斥的。 - ddavison

0
ArrayList<String> tabHandles1 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabHandles1.get(index));

您可以将索引的值传递给位置,或者通过使用arrayList tabhandles1迭代所有选项卡


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接