本文介绍了如何使用Selenium(java)在IE中打开新标签页,然后在该标签页(而非窗口)中打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用selenium(java)在IE中打开新标签页并在该标签页中打开网址(而不是窗口)?我正在使用以下代码打开新标签页?

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..

问候沙申克·戈亚尔(Shashank Goyal)

RegardsShashank Goyal

推荐答案

您需要使用

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中打开新标签页,然后在该标签页(而非窗口)中打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 13:55