0

I am working on automation testing using Selenium java. For my scenario first opening the login page and after providing credentials and clicked Login button current browser (with login page) closes and a new IE window open with Home page. In java code I am getting the driver with login page url and from there providing username and pwd then click on login page working fine, but after that as the page closes and new IE window opened while I am trying to work on the home page I am getting noSuchWindowException or Null handle like these. Can you help me in getting the new window handle for Home page?

My codes are like below:

...
System.setProperty("webdriver.ie.driver", pathToDriver);
driver = new InternetExplorerDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("<Login Page URL>");

driver.findElement(By.id("userName")).sendKeys("Admin");
driver.findElement(By.id("password")).sendKeys("Admin");
driver.findElement(By.className("loginButton")).click();

driver.get("<Home Page URL>");
...

Thanks,

Surodip

2
  • If new IE window opened why are you doing driver.get("<Home Page URL>");? Commented Mar 4, 2020 at 11:09
  • Actually I want to get the control/handle of the newly opened window/page, so I was trying to achieve that by driver.get("<Home Page URL>"); but at that point I was getting error. So that statement is wrong and something else need to be done, but couldn't get what. Commented Mar 4, 2020 at 12:54

3 Answers 3

0

Selenium driver.getWindowHandles() will return Set and there is no method of Set to get last window handle but TreeSet has, so convert and use its last() method to use.

String lastWindowHandle = new TreeSet<>(driver.getWindowHandles()).last()
driver.SwitchTo().Window(lastWindowHandle);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sumit...will try that
0

You need to check if the navigation from the login page to the home page completed within time while your operation to get the home page handle was called. You might need to delay the call using Wait..

Comments

0

A simpler solution could be this:

driver.SwitchTo().Window(driver.WindowHandles.Last());

.Last() will just switch to the most recently opened window, which should be the one that just opened when you clicked the link.

1 Comment

will check your solution Viki...Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.