0

I am trying to change my driver's focus to the new window that pops up as a result of clicking a link. The only problem is that both the parent window and the pop-up window have the same handle - for example - the title of both is " <Company Name - Abbreviation 1>

How would I use Selenium JAVA to get focus on the pop-up window a different way than what I have used?

`

String originalWindow = driver.getWindowHandle();
        assert driver.getWindowHandles().size() == 1; driver.findElement(By.xpath("xpath goes here")).click(); // Training
    

        // Loop through until we find a new window handle
        for (String windowHandle : driver.getWindowHandles()) {
            if (!originalWindow.contentEquals(windowHandle)) {
                driver.switchTo().window(windowHandle);
                driver.manage().window().maximize();

            }
        }

`

Thanks, Everyone!

`

String originalWindow = driver.getWindowHandle();
        assert driver.getWindowHandles().size() == 1;
        driver.findElement(By.xpath("xpath goes here")).click(); // Training
        Thread.sleep(1000);

        // Loop through until we find a new window handle
        for (String windowHandle : driver.getWindowHandles()) {
            if (!originalWindow.contentEquals(windowHandle)) {
                driver.switchTo().window(windowHandle);
                driver.manage().window().maximize();

            }
        }

`

I tried the above code, but both windows sadly have the same title. I want to switch focus to the new window so I can then interact with the new one.

  • The title doesn't matter... but it sounds like there is only 1 window/tab? So you don't need to change focus/switch driver. Just do your next thing... OR, is it possible you have a bug in your code? If there is a new window/tab here, and you getWindowHandles() before the new window/tab is opened then your loop will fail. You do have a .click() happening after getting the handles... is that the click that opens the new window/tab? You may also want to use a webdriverwait and expected condition of numberOfWindowsToBe... – pcalkins Oct 27 '22 at 19:16
  • Hey, Thanks for the response! There is actually two windows open after I click the link. Once I click the link the second window opens, but they both have the same title. I'm able to maximize the window that pops up - but when trying to interact or click any other element on the popup Window (new window) my mouse and driver focus is still on the parent window. – Lukja.E Oct 27 '22 at 20:09
  • they are separate contexts regarding focus. Include the code you are using when trying to interact with elements. Try/catch those calls and include any exceptions. You should use a webdriverwait on those too... Selenium will not automatically wait for the page to load after the new window/tab is opened. Including that window's HTML markup in your post is a good idea, too. Easier to troubleshoot if you include code + markup you are attempting to target. Also note that some site's do a popunder.. (new window/tab gets the old content) – pcalkins Oct 27 '22 at 21:58

0 Answers0