WebDriver.getWindowHandle() method

2020-02-11 06:02发布

问题:

I'm new to Selenium learning. WebDriver.getWindowHandle() documentation is 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.

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

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. However my program still returns handle to PAGE1.

Selenium v2.43

Reproducible on Firefox and Chrome both.

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

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

回答1:

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.

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.

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

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

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

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



回答2:

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.

See this example from 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;
    }
  }


回答3:

When you open the new window, the WebDriver doesn'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).



回答4:

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);
    }


回答5:

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.

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.

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