-2

this is my code wait method is not working switching over windows is not working please help me to get over this issue

i am using selenium 3.01 jars

import java.util.concurrent.ForkJoinPool.ManagedBlocker;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WindowHandling extends BaseClass
{
public static void main(String args[])
{

    System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"\\driver\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();

    //System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\driver\\chromedriver.exe");
    //WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("file://D:/8850OS_Code/Chapter 3/HTML/Window.html");
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    String window1 = driver.getWindowHandle();
    System.out.println("First Window Handle is: " + window1);
    WebElement link = driver.findElement(By.linkText("Google Search"));
    link.click();
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    String window2 = driver.getWindowHandle();
    System.out.println("Second Window Handle is: " + window2);
    System.out.println("Number of Window Handles so for: "+           driver.getWindowHandles().size());
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
    driver.switchTo().window(window1);
    System.out.println("task done ");
}

}

thank you

  • Possible duplicate of [this](http://stackoverflow.com/questions/34422661/selenium-implicitlywait-not-working) SO question. – Jeroen Heier Feb 13 '17 at 15:53
  • 1
    First: And what is problem? Second: Do you know that Selenium wait is something different then sleep actual thread? – Hrabosch Feb 13 '17 at 16:15

1 Answers1

0

Switching windows doesn't work for you because you are switching to the currently focused (first) window. If you check window1 and window2 values you will see they are the same: the window handle of the first window. To switch to the new window you need to switch to the handle that is different from the current window handle

String window1 = driver.getWindowHandle();
for (String windowHandle : driver.getWindowHandles()) {
    if (!windowHandle.equals(window1)) {
        driver.switchTo().window(windowHandle);
    }
}

String window2 = driver.getWindowHandle(); // now it will be the window handle of the second window

And to close the new window and switch back

driver.close();
driver.switchTo().window(window1);

As for the wait, implicitlyWait is defined one time after creating the WebDriver, it will be valid for the entire life span of th driver and it means the driver will wait up to the specified amount of time until the elements are found when using driver.findElement(). You can see more about waits here.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • code above is showing error i am new to cam you suggest easy way to learn selenium or if you can teach me any suggestion is most welocme – MasterTester Feb 14 '17 at 15:22
  • 1
    @MasterTester You have to be more specific about what the error is. And the best way to learn is to try. – Guy Feb 14 '17 at 16:09
  • if you don't mind , is there way apart from this where i can contact you so that i can share my code with you , i can learn something . if you don't want then sorry in advance – MasterTester Feb 17 '17 at 16:21