findElements()返回NoSuchElementException而不是返回空列表

时间:2015-07-02 16:51:58

标签: java selenium xpath selenium-webdriver webdriver

以下是代码:

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]"))

现在根据我的理解,这应该返回Web元素列表或空列表[考虑xpath在语法上是正确的。]

相反,抛出一个异常的[NoSuchElementException]并带有令人困惑的消息,因为&#34;返回了一个意外的错误&#34;。 以下是例外,

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}]

用于元素等待的代码就像是,

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;
}

[附加信息]但是,

当我等待一段时间[确保元素加载]&amp;写为

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

点击该元素。

问题的任何原因/解决方案?? [findElements()返回NoSuchElementException]

1 个答案:

答案 0 :(得分:3)

更新

为什么要在already那里编写自己的等待算法来重新发明轮子。 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()]")));