0

how to close tab automatically if there are more than 5 tabs open. I can open different tabs using selenium now I have to check if there is more than 5 tab then it should close by its own. I tried to go to the task manager using winium but I don't understand what I have to do next.

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26

1 Answers1

0

Where you are initiating to open a URL, add below code

Set<String> WHandles= driver.getWindowHandles();
int WHandleCnt= WHandles.size();
if(WHandleCnt>5){
  for(int i=0;i<WHandleCnt;i++){
    String CurrentTab= WHandles.iterator().next();
    driver.switchTo().window(CurrentTab);
    driver.close();
  }
}
Sonali Das
  • 943
  • 1
  • 7
  • 24
  • I want to check Task Manager automatically for more chrome tabs and close the ones open 5 hours ago. Can you help me with this? – Kishan kumar Apr 02 '21 at 00:03