WebDriver.getWindowHandle () Method

I am new to teaching Selena. WebDriver.getWindowHandle () the documentation is not very clear to me, and the example does not work, as indicated in the book, so I thought about the confirmation of the value returned by this method.

1) Let's say I'm on page PAGE1. Therefore, getWindowHandle () should return a handle to PAGE1. (Correctly)

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

Selenium v2.43

Playable in Firefox and Chrome both.

Question: What is the exact value returned by 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

+4
5

, WebDriver. getWindowHandles.

. 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

, WebDriver . switchTo() , ( getWindowHandles() , ).

+3

getWindowHandle() , webDriver. -. , URL.

getWindowHandles() ( 's') , -, . , , , .

SwitchTo().Window("handle") .

SwitchTo().Window("mywindowID"), .

SwitchTo().Window("") / .

SwitchTo().Frame("popupFrame") , , webdriver.

+2

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

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

Selenium 2.53.1 firefox 47.0.1 WebDriver Java: / . . , , "driver.getWindowHandles()" , . , Chrome .

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 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 CURRENT TAB
 }

, , .

0

All Articles