gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:15:19 25 4
gpt4 key购买 nike

以下是代码:

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

或者

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

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

相反,会抛出异常[NoSuchElementException],并带有令人困惑的消息“返回意外错误”。以下是异常(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}]

用于元素等待的代码如下:

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

[其他信息]但是,

当我硬等待一段时间[以确保元素已加载]并写为

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

该元素被点击。

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

最佳答案

更新

当它是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()]")));

关于java - findElements() 返回 NoSuchElementException 而不是返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com