0

I am clicking on a button in my code "Go" which opens a new window. When I check the task manager I can see the instances of IE as 2 but getWindowHandles() is unable to get the handle of the newly opened window and just returns the parent window handle.

The code that I am using to do this is:

SeleniumUtils.clickOnElement(webDriver, By.xpath("//input[@name='Go' and @value='Go']"), "Click on Go", reporter);
SeleniumUtils.waitLoading(SeleniumUtils.WAIT_LONG);

List<String> windowsList = new ArrayList<>();
String parentWindow = webDriver.getWindowHandle();
windowsList.add(parentWindow);

String emailWindow = SeleniumUtils.changeDriverToTheNewWindow(webDriver, windowsList, windowsList.size()+1);
windowsList.add(emailWindow);

To change to the new window, I have written a function:

public static String changeDriverToTheNewWindow(WebDriver webDriver, List<String> previousWindows, int numberOfWindows) {
    try {
        JavascriptExecutor jsExecuter = (JavascriptExecutor) webDriver; 
        Set<String> windows = Collections.emptySet();
        try 
        {
            WebDriverWait wait = new WebDriverWait (webDriver, 5);
            wait.until(ExpectedConditions.numberOfWindowsToBe(numberOfWindows));    
            windows = webDriver.getWindowHandles();
        }catch(Exception e) {
            lLogger.error("Error getting the window handles.", e);
            windows = webDriver.getWindowHandles();
        }

        for (String windowId : windows) {
            if (!previousWindows.contains(windowId)) {
                webDriver.switchTo().window(windowId);
                jsExecuter.executeScript("window.focus");
                //changeDriverToWindow(webDriver, windowId);
                return windowId;
            }
        }
    } catch (Exception e) {
        lLogger.error("Error changing driver to the new window!", e);
    }
    return null;
}

The HTML & JS code that opens the window is:

<td class="xyz-column">

<link href="../Content/css/XYZ.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
    function onMouseOver(control) {
        control.src = '../Content/images/lookup.png';
    }
    function onMouseOut(control) {
        control.src = '../Content/images/btn_lookup.png';
    }
</script>

<span class="xyz-link" id="body_XYZSelector_XYZLookup_LookupName" style="width: 175px; height: 18px; display: inline-block;" onclick="OpenXYZ(GetCurrentLookupId(this),'42e7a262-36ef-e911-80f1-005056aea4ce','10001'); return false;"> SHARANA </span>
<img class="xyz-lookup" id="body_XYZSelector_XYZLookup_LookupImage" onmouseover="onMouseOver(this);" onmouseout="onMouseOut(this);" onclick="OpenLookUp(this);" alt="Browse" src="../Content/images/btn_lookup.png" entitytype="xyz">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenId" id="body_XYZSelector_XYZLookup_LookupHiddenId" type="hidden" value="56765">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenName" id="body_XYZSelector_XYZLookup_LookupHiddenName" type="hidden" value="SHARANA">

<input name="ctl00$body$XYZSelector$XYZLookup$LookupHiddenId" id="body_XYZSelector_XYZLookup_LookupHiddenId" type="hidden" value="fd878f2b-90a2-4bf0-8a00-daa007b64b7d">


</td>
PratikshaB
  • 37
  • 6
  • put some wait before using getWindowHandles .. try first with Thread.sleep – Shubham Jain Oct 04 '19 at 11:24
  • @Shubham I tried `Thread.sleep(1000)` before `getwindowHandles()` still not working. – PratikshaB Oct 04 '19 at 11:39
  • try to update your iedriver, may the version issue then – Shubham Jain Oct 04 '19 at 11:41
  • @ShubhamJain I don't think the version is an issue, because I can successfully switch between other windows using the same one. – PratikshaB Oct 04 '19 at 11:43
  • Did you try to debug the code and verify whether you are getting the correct window handle or not? if not, you can have a try. Is that window taking more time to get completely load? If so than, try to add some extra time to check whether it helps or not. you said, you are able to switch to other windows. so is there any difference between these windows? Let us know about that may help to narrow down the issue. – Deepak-MSFT Oct 04 '19 at 14:25
  • @Deepak-MSFT Yes I have verified that I am getting the correct Parent window handle. I did that by adding `wait.until()` an element on the parent loads. It doesn't throw an error. As for the difference between the windows, the other windows that I am able to switch to are popups within the parent (these do not reflect in the task manager) but the `EmailWindow` is a new tab/window which is reflected in the task manager and shows the instances of IE running as 2. – PratikshaB Oct 04 '19 at 15:27
  • Try to switch to new window using the name of the window explicitly without using above function to check whether it helps to move to that Email window or not.As another option you can loop through all the windows and try to match the title of the window to switch to specific window. If it is last window than you can use driver.SwitchTo().Window(driver.WindowHandles.Last()); Ref: https://www.toolsqa.com/selenium-webdriver/switch-commands/ and https://www.seleniumeasy.com/selenium-tutorials/perform-operations-on-new-window-using-webdriver – Deepak-MSFT Oct 07 '19 at 05:37
  • @Deepak-MSFT I cannot get that working, because `getWindowHandles()` is not returning the handle of that window. I am also unable to access the window using `getTitle()` Can you tell me how I can loop through all the IE windows present? – PratikshaB Oct 07 '19 at 13:14
  • You can try to refer this example will help to loop through IE windows and get their title. It also shows you how to get the handle of window and switch to that window. Ref: https://textuploader.com/1kray output: https://i.postimg.cc/ZRBj3D8z/296.png – Deepak-MSFT Oct 08 '19 at 05:47
  • @Deepak-MSFT Still not working for me. – PratikshaB Oct 09 '19 at 09:07
  • What did not worked? please provide detailed information. only the word not working not give any idea. – Deepak-MSFT Oct 09 '19 at 09:16
  • @Deepak-MSFT The example in the link that you mentioned requires me to firstly open the window using some URL, but the URL of this particular window changes every time. I anyways have the window opening without that part but it needs me to get the handle of the window first before I can do any other manipulations on it (Such as get the title). My issue is I am unable to get handle of a newly opened window/ tab. – PratikshaB Oct 09 '19 at 10:11
  • Here, I want to confirm with you that the Window whose handle you wan to get using web driver is created using web driver or it is created using any other way? If window is not created using that web driver than it may not be able to found it. – Deepak-MSFT Oct 10 '19 at 07:34
  • @Deepak-MSFT Yes the window is created using the same webDriver I am using to get the handles. – PratikshaB Oct 11 '19 at 09:15
  • In that case, you just need to use the last loop from my code and place it in your code and check whether it is able to find the window or not. – Deepak-MSFT Oct 11 '19 at 09:17
  • @Deepak-MSFT I am already using that loop. Please check the last loop in the `changeDriverToTheNewWindow()` in my code. It's not able to find the window!! – PratikshaB Oct 11 '19 at 10:04
  • Not sure why it is not working because there is no HTML code or site to test with the selenium automation code. Maybe we need to access the site to see why it is not working. We need to make a test with that site to see the actual result. without that we are not able to provide any further suggestions. Thanks for your understanding. – Deepak-MSFT Oct 14 '19 at 06:26
  • @Deepak-MSFT Can you tell me if it is possible to switch from one IE window to a newly created/ opened IE window using selenium? Or is it just for pop-ups or dialog boxes? – PratikshaB Oct 14 '19 at 09:34
  • Yes, The example I suggested you is the way to do it. – Deepak-MSFT Oct 14 '19 at 09:36
  • @Deepak-MSFT what part of HTML do you need to debug this? I can try posting a sample from my application? – PratikshaB Oct 15 '19 at 10:45
  • We need a HTML and JS code which opens that window. It can be helpful for testing with Selenium code. we will try to check whether we can get that windows handle or not. – Deepak-MSFT Oct 17 '19 at 01:30
  • @Deepak-MSFT I have edited my question and added the HTML and JS code. Can you please take a look at it? – PratikshaB Oct 18 '19 at 09:35
  • You still need to post a JS code which open the window. Many JS functions are missing which you are using in your HTML. – Deepak-MSFT Oct 18 '19 at 09:43

1 Answers1

0

I found the solution to my problem. Just make sure you follow all the steps as you see at Solution answered by @DebanjanB.

To be more specific, in my case it was the ... InternetExplorerOptions options = new InternetExplorerOptions(); options.merge(cap); WebDriver driver = new InternetExplorerDriver(options);

as you can see at Solution 1 and

ieCapabilities.setCapability("requireWindowFocus", false);

as you can see at Solution 2

After adding these two, the driver was able to get the handle of the window correctly.

PratikshaB
  • 37
  • 6