0

I am working on a application which is desired for IE only. Once after login into application a new new window is opened which is the home page. Now my issue is I am not able to locate any element in homepage through IE driver. Not sure if the browser is looking for the element in previous window.

My method:

public void awb_enquiry() throws Exception {

    String handles= driver.getWindowHandle();
    System.out.println(handles);
    driver.switchTo().window(handles);
    driver.findElement(By.xpath("//*[text()='Select']")).click();
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Navas
  • 15
  • 1
  • 6
  • I tried all possible things to locate element in the current page but no luck. I tried checking the elements in previous elements to locate and that is also not happening.I have tried all the things as mentioned below: – Navas Jul 24 '18 at 09:04
  • 1. Waits(Implicit, Explicit) 2. try/catch exception for handling 3. unique xpath that locates the element alone 4. Assert using finElement to find size 5. windowHandle(), windowHandles() 6. Action Class 7. Tried to get URL/ Title of the page 8. Iterator to navigate from one window to another 9. Using IsDisplayed(),to check Element Present – Navas Jul 24 '18 at 09:07

3 Answers3

0

I also faced similar issues, Unable to fetch the locator, but the locator do exist. Here I suggest working solution, Just give a try with locating web element using JavaScript.

Alternative way to fetch locator, if driver.findElement is not working means, use the below java script

public WebElement getIsConvertedCheckBoxElement() {
JavascriptExecutor js = getDriver();
String script = "return document.getElementById('conversion');";
return (WebElement) js.executeScript(script);
}
selvi
  • 1,271
  • 2
  • 21
  • 41
0

As @JimEvans mentioned in his comments it is to be noted that,

While the datatype used for storing the list of handles may be ordered by insertion, the order in which the WebDriver implementation iterates over the window handles to insert them has no requirement to be stable. The ordering is arbitrary.

So, we have to induce an WebDriverWait and then collect the window handles every time we open a new tab/window and finally iterate through the window handles and switchTo().window(newly_opened) as per the following example:

  • Sample Code:

    System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
    WebDriver driver =  new InternetExplorerDriver();
    driver.get("http://www.google.com");
    String first_tab = driver.getWindowHandle();
    System.out.println("Working on Google");
    ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    while(i1.hasNext())
    {
        String next_tab = i1.next();
        if (!first_tab.equalsIgnoreCase(next_tab))
        {
        driver.switchTo().window(next_tab);
    
        System.out.println("Working on Facebook");
        }
    }
    
  • Console Output:

    Working on Google
    Working on Facebook
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1
String beforeLogin = driver.getWindowHandle();
/*
*  Perform Login
*/ 
ArrayList<String> windows = new ArrayList<String>(driver.getWindowHandles()); 
// Now switch to 2nd window 
driver.switchTo.window(windows.get(1)); 
driver.findElement(By.xpath("//*[text()='Select']")).click();
  • 1
    This answer is incorrect. The `getWindowHandles` method does not return the handles in any order. Getting the handle with index 1 from the set is **not** guaranteed to be the newly opened window. – JimEvans Jul 05 '18 at 11:51
  • Hi Shiva, thanks for your answer. But still am getting error as: – Navas Jul 05 '18 at 11:52
  • java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at stepdefinitions.AppLogin.awb_enquiry(AppLogin.java:58) at ✽.Then User enquires AWB status(cmsweb_001_login.feature:6) – Navas Jul 05 '18 at 11:53
  • @Navas So either new window not opened or need sleep time after new window is opened. If your are sure that window is opened then put thread.sleep(2000) after window opened and try to switch to that window. – Shiva Krishna Chippa Jul 05 '18 at 12:25
  • @JimEvans - As per the question, there will be only two windows after login. So I believe my syntax will work as it is been working for me for the past 4 years in my application. – Shiva Krishna Chippa Jul 05 '18 at 12:36
  • @JimEvans was pretty right to point out that `getWindowHandles` method does not return the handles in any order. – undetected Selenium Jul 05 '18 at 14:49
  • my getWindowHandles says the window count(size) as 1. also it's not printing the .getTitle() or .getUrl() also to know in which page it is searching for element. Not sure how to confirm if my code is searching in right window – Navas Jul 06 '18 at 12:21