-1

I know method getWindowHandles() return un-ordered Set. is there any best practice to handling this?

It not a big deal when I have two tab, but I got trouble when I have more than 2 tab open and use toArray()[index] to move between tab because everytime run test, index are different eventhought new tab opened with the same steps.

Is there any possibilities to return index in sequence (based on timestamp the new tab are openen, maybe?) to guarantee that i can move to specific tab?

J. Doem
  • 619
  • 7
  • 21
  • Create and track your own ordered set. Put main window in there first. As you open each new window, add it to the set. – JeffC Apr 20 '19 at 01:18

1 Answers1

0

getWindowHandles() method actually returns a set of string which is unordered.

In order to change switch between the tabs based on index you can move the set of string to a List(which is ordered) like below and select by index:

Set<String> allWindows=driver.getWindowHandles(); 
List<String> listOfAllWindows=new ArrayList<String>();
listOfAllWindows.addAll(allWindows);
driver.switchTo().window(listOfAllWindows.get(index)); 
System.out.println(driver.getTitle());
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Fury
  • 142
  • 1
  • 12