如何使用 selenium (java) 在 IE 中打开新选项卡并在该选项卡(不是窗口)中打开一个 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22820527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:06:51  来源:igfitidea点击:

How to open new tab in IE using selenium (java) and open a url in that tab (not window)

javaseleniumtabs

提问by user3004247

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?

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

Can anyone tell me, how to shift control to new tab so that the facebook link opens in that new tab.

谁能告诉我,如何将控制权转移到新标签,以便在该新标签中打开 facebook 链接。

Hi

你好

I using Selenium Web-Driver Version 2.40 and IE 11.0

我使用 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)

I am not able to open facebook on new tab of same window..

我无法在同一窗口的新标签页上打开 facebook..

Regards Shashank Goyal

问候 Shashank Goyal

回答by ddavison

You'll need to use

你需要使用

driver.switchTo().window(String)

to switch to the window that has come up just as you would with opening a new window.

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

回答by Ravi

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

u can pass the place the value of index or can iterate to all the tab using arrayList tabhandles1

您可以传递索引值的位置,也可以使用 arrayList tabhandles1 迭代到所有选项卡