Shoaib Shaikh
Shoaib Shaikh

Reputation: 353

findElements() returns NoSuchElementException instead of returning empty list

Following is code :

List<WebElement> listOfAllMatchingElements = driver.findElements(By.xpath(".//*[@id='e1MMenuRoot']/div/div[last()]"))

OR

 List<WebElement> listOfAllMatchingElements = driver.findElements(By.xpath(".//*[@id='e1MMenuRoot']/div/div[5]"))

Now as per my understanding, this should return list of Web elements Or an empty list[Considering the xpath is syntactically correct].

Instead, an exception[NoSuchElementException] is thrown with a confusing message as "returned an unexpected error". Following is the exception,

org.openqa.selenium.NoSuchElementException: Finding elements with xpath == .//*[@id='e1MMenuRoot']/div/div[5]returned an unexpected error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.37 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'OTINWISRCDT050', ip: '172.24.187.38', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_71'
*** Element info: {Using=xpath, value=.//*[@id='e1MMenuRoot']/div/div[5]}
Session ID: a45d6015-f529-4e85-924e-3214076d59e8
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=true, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://localhost:31736/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]

The code used for element wait is like,

public boolean customElementWaitWithTimeoutWithProperties(WebDriver UtilDriver,By locatorWithLocation,int Timeout)
{
    WebDriver driver = UtilDriver;
    boolean elementFound=false;

    int i=1;
    try
    {

        while(i<=Timeout )
        {
            if(((Boolean.compare(elementFound,false)))==0)
            {


                List<WebElement> listOfAllMatchingElements = driver.findElements(locatorWithLocation);
                if(!(listOfAllMatchingElements.isEmpty()) && (((Boolean.compare(elementFound,false)))==0))
                {
                    if(listOfAllMatchingElements.size()>1)
                    {
                        log.info("Common Methods :customElementWaitWithTimeout: More than one element is found for given location, check the location !!");

                        elementFound=false;
                        break;
                    }
                    else if(listOfAllMatchingElements.size()==1 && (((Boolean.compare(elementFound,false)))==0))
                    {


                        log.info("Common Methods :customElementWaitWithTimeout: Element  found on "+i+" attempt !!");
                        elementFound=true;


                        break;

                    }
                }
                else if ((listOfAllMatchingElements.isEmpty()))
                {

                    log.info("Common Methods :customElementWaitWithTimeout: Element  is not found on "+i+" attempt!!");

                }
                Thread.sleep(1200);
            }
            i=i+1;
        }


    }
    catch(Exception elementFoundCheck)
    {
        log.error("Common Methods[customElementWaitWithTimeout]: Exception caught !!");
        elementFoundCheck.printStackTrace();
    }

    return elementFound;
}

[Additional info]However,

when i put a hard wait for certain time[To make sure element is loaded] & write as

driver.findElement(By.xpath(".//*[@id='e1MMenuRoot']/div/div[5]")).click();

The element is getting clicked.

Any reason/solution for the problem ??[findElements() returning NoSuchElementException]

Upvotes: 0

Views: 3679

Answers (1)

Madhan
Madhan

Reputation: 5818

Update

Why are you reinventing the wheel by writing your own wait algorithm when it's already there. Refer

  //wait for 20 seconds 
    WebDriverWait wait = new WebDriverWait(driver, 20);
    List<WebElement> listOfAllMatchingElements=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(".//*[@id='e1MMenuRoot']/div/div[last()]")));

Upvotes: 3

Related Questions