Java WebDriver.getWindowHandle() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25871042/
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-11 01:22:36  来源:igfitidea点击:

WebDriver.getWindowHandle() method

javaselenium-webdriverwebdriver

提问by Ketan

I'm new to Selenium learning. WebDriver.getWindowHandle() documentationis not very clear to me and the example is not working as given in the book, so I thought of confirming the value returned by this method.

我是硒学习的新手。WebDriver.getWindowHandle()文档对我来说不是很清楚,并且该示例没有按照书中给出的方式工作,因此我想确认此方法返回的值。

1) Let's say I am on page PAGE1. So getWindowHandle() should return handle to PAGE1. (Correct)

1) 假设我在页面 PAGE1。所以 getWindowHandle() 应该返回 PAGE1 的句柄。(正确的)

2) Now from this page, I go to PAGE2 (by hyperlink and opening a new window). My book says now getWindowHandle() should return handle to PAGE2. Howevermy program still returns handle to PAGE1.

2) 现在从这个页面,我转到 PAGE2(通过超链接并打开一个新窗口)。我的书说现在 getWindowHandle() 应该返回 PAGE2 的句柄。但是我的程序仍然返回 PAGE1 的句柄。

Selenium v2.43

硒 v2.43

Reproducible on Firefox and Chrome both.

可在 Firefox 和 Chrome 上重现。

Question: What is the exact value that getWindowHandle() should return?

问题:getWindowHandle() 应该返回的确切值是多少?

WebDriver wd = new ChromeDriver();
wd.get("file://D:/Projects/Selenium/Startup/web/ch3/switch_main.html");

String h1 = wd.getWindowHandle();// original handle
System.out.println("First handle = " + h1);

WebElement clickhere = wd.findElement(By.id("clickhere"));
clickhere.click();//moved to a new child page<

String h2 = wd.getWindowHandle();
System.out.println("Second handle = " + h2);// this handle is not different than h1

回答by isalgueiro

If the link opens a new window you should have a new window handle in the WebDriver. You can loop current window handles with getWindowHandles.

如果链接打开一个新窗口,您应该在WebDriver. 您可以使用getWindowHandles循环当前窗口句柄。

See this example from http://www.thoughtworks.com/products/docs/twist/13.3/help/how_do_i_handle_popup_in_selenium2.html

请参阅http://www.thoughtworks.com/products/docs/twist/13.3/help/how_do_i_handle_popup_in_selenium2.html 中的此示例

  String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
  WebDriver popup = null;
  Iterator<String> windowIterator = browser.getWindowHandles();
  while(windowIterator.hasNext()) { 
    String windowHandle = windowIterator.next(); 
    popup = browser.switchTo().window(windowHandle);
    if (popup.getTitle().equals("Google") {
      break;
    }
  }

回答by Sizik

When you open the new window, the WebDriverdoesn't automatically switch to it. You need to use the switchTo()method to switch to the new window, either using the name of the new window, or its handle (which you can get with getWindowHandles()and searching for the one that's not the current window).

当您打开新窗口时,WebDriver不会自动切换到它。您需要使用该switchTo()方法切换到新窗口,使用新窗口的名称或其句柄(您可以使用getWindowHandles()并搜索不是当前窗口的那个)。

回答by Chuck Brown

getWindowHandle()will get the handle of the page the webDriver is currently controlling. This handle is a unique identifier for the web page. This is different every time you open a page even if it is the same URL.

getWindowHandle()将获得 webDriver 当前控制的页面的句柄。此句柄是网页的唯一标识符。每次打开页面时,即使它是相同的 URL,这也是不同的。

getWindowHandles()(don't forget the 's') will give you all the handles for all the pages that the web driver understands are open. Note that when you put these in a list they are listed in the order that they have been opened.

getWindowHandles()(不要忘记's')将为您提供网络驱动程序理解为打开的所有页面的所有句柄。请注意,当您将它们放入列表中时,它们会按照打开的顺序列出。

You can use SwitchTo().Window("handle")to switch to the window you desire.

您可以使用SwitchTo().Window("handle")切换到您想要的窗口。

You can use SwitchTo().Window("mywindowID"), if you know the window ID.

您可以使用SwitchTo().Window("mywindowID"), 如果您知道窗口 ID。

SwitchTo().Window("")will always go back to the base/main window.

SwitchTo().Window("")将始终返回基本/主窗口。

SwitchTo().Frame("popupFrame")will get to the Popup that came from the window the webdriver is currently controlling.

SwitchTo().Frame("popupFrame")将到达来自 webdriver 当前控制的窗口的弹出窗口。

回答by OPTIMUS

I have used this code for my project

我已将此代码用于我的项目

String oldTab = driver.getWindowHandle();

public static void switchingToNewTabUsingid(WebDriver driver,WebDriverWait wait,String id,String oldTab)
    {
        wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));
        driver.findElement(By.id(id)).click();
        ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
        newTab.remove(oldTab);
        driver.switchTo().window(newTab.get(0));
    }

//Perfrom Opeartion here on switched tab

//在这里执行切换选项卡上的操作

public static void comingBackToOldTab(WebDriver driver,String oldTab)
    {
        driver.close();
        driver.switchTo().window(oldTab);
    }

回答by Charles Woodson

With Selenium 2.53.1 using firefox 47.0.1 as the WebDriver in Java: You need to open the separate windows/browsers in it's own driver. I have having the same problem. No matter how many windows or tabs I opened, "driver.getWindowHandles()" would only return one handle so it was impossible to switch between tabs. I found Chrome worked way better for me.

在 Selenium 2.53.1 中使用 firefox 47.0.1 作为 Java 中的 WebDriver:您需要在它自己的驱动程序中打开单独的窗口/浏览器。我有同样的问题。无论我打开多少个窗口或选项卡,“driver.getWindowHandles()”都只会返回一个句柄,因此无法在选项卡之间切换。我发现 Chrome 对我来说效果更好。

Once I started using Chrome 51.0, I could get all handles. The following code show how to access multiple drivers and multiple tabs within each driver.

一旦我开始使用 Chrome 51.0,我就可以得到所有的处理。以下代码显示了如何访问多个驱动程序和每个驱动程序中的多个选项卡。

// INITIALIZE TWO DRIVERS (THESE REPRESENT SEPARATE CHROME WINDOWS/BROWSERS)
driver1 = new ChromeDriver();
driver2 = new ChromeDriver();

// LOOP TO OPEN AS MANY TABS AS YOU WISH
for(int i = 0; i < TAB_NUMBER; i++) {
   driver1.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
   // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB
   Thread.sleep(100);

 // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS
 ArrayList tabs1 = new ArrayList<String> (driver1.getWindowHandles());

 // REPEAT FOR THE SECOND DRIVER (SECOND CHROME BROWSER WINDOW)

 // LOOP TO OPEN AS MANY TABS AS YOU WISH
 for(int i = 0; i < TAB_NUMBER; i++) {
    driver2.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
    // SLEEP FOR SPLIT SECOND TO ALLOW DRIVER TIME TO OPEN TAB
    Thread.sleep(100);

 // STORE TAB HANDLES IN ARRAY LIST FOR EASY ACCESS
 ArrayList tabs2 = new ArrayList<String> (driver2.getWindowHandles());

 // NOW PERFORM DESIRED TASKS WITH FIRST BROWSER IN ANY TAB
 for(int ii = 0; ii <= TAB_NUMBER; ii++) {
    driver2.switchTo().window(tabs2.get(ii));
    // LOGIC FOR THAT DRIVER'S CURRENT TAB
 }

 // PERFORM DESIRED TASKS WITH SECOND BROWSER IN ANY TAB
 for(int ii = 0; ii <= TAB_NUMBER; ii++) {
    drvier2.switchTo().window(tabs2.get(ii));
    // LOGIC FOR THAT DRIVER'S CURRENT TAB
 }

Hopefully that gives you a good idea of how to manipulate multiple tabs in multiple browser windows.

希望这能让您很好地了解如何在多个浏览器窗口中操作多个选项卡。