1

I am clicking on an icon , which results in new window. I want to move to that window ,and perform some operation, close that window, and then again switch back to Parent window.

I have written the below code to perform my action . But it seems not working and my method is not getting call as well .Please review the below code and help me .

public void collection_title() {

        // It will return the parent window name as a String
        String parent = driver.getWindowHandle();
        Set<String> s = driver.getWindowHandles();
        // Now iterate using Iterator
        Iterator<String> I1 = s.iterator();
        while (I1.hasNext()) {

            String child_window = I1.next();


            if (!parent.equals(child_window)) {
                driver.switchTo().window(child_window);

                System.out.println("the window name is "+driver.switchTo().window(child_window).getTitle());

               
            }

        //Code is generating random string

        String uuid = UUID.randomUUID().toString();
        edit_coln.sendKeys(uuid);

        System.out.print("text got entered ");

        }
}
tsamridh86
  • 1,480
  • 11
  • 23
swagatika
  • 47
  • 4
  • Why do you try to force it to switch to the `parent` after the loop is completed? Either the code is not provided in the question, or I don't see any logic that finally does `driver.switchTo().window(parent)`. Since, you already have the `parent` variable, so it should not be a problem. – tsamridh86 Dec 04 '20 at 03:43

2 Answers2

0

You have to Handle windows in selenium.(There are parent window and child window , you can jump(switchTo) from window to window to perform some actions whatever you need)

Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

Get all the opened window handle's and traverse through it and perform some operation and close window

String parent = driver.getWindowHandle();
Set<String> s = driver.getWindowhandles();

Iterator<String> itr = s.iterator();
   while(itr.hasNext()){
    String sWindow = itr.next().toString();
    driver.switchTo().window(sWindow); 
   } 
//perform some operations in your case it's clicking on button
 new WebDriverWait(driver, 10, 
 100).until(ExpectedCondition.elementToBeClickable(By.xpath("element xpath"))).click();
 driver.close();
 driver.switchTo.window(parent); 
YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20