Selenium 预期条件。智能体的出现慢于多少?
webDriver如何知道定位器定位了多少个元素以显示?Selenium 预期条件。智能体的出现慢于多少?,selenium,selenium-webdriver,Selenium,Selenium Webdriver,webDriver如何知道定位器定位了多少个元素以显示?presenceOfAllElementsLocatedBy将检查至少一个元素是否存在,然后返回与所提供定位器匹配的所有元素 这是方法实现的官方文档 检查是否至少存在一个元素的期望 在网页上 定位器用于查找元素 找到WebElement后返回WebElement的列表 这里是java文档的官方链接。 如果查看代码,它只会获取与提供的定位器匹配的所有元素。它捕获的唯一内容是StaleElementReferenceException。下面是完
presenceOfAllElementsLocatedBy
将检查至少一个元素是否存在,然后返回与所提供定位器匹配的所有元素
这是方法实现的官方文档
检查是否至少存在一个元素的期望
在网页上
定位器用于查找元素
找到WebElement后返回WebElement的列表
这里是java文档的官方链接。
如果查看代码,它只会获取与提供的定位器匹配的所有元素。它捕获的唯一内容是
StaleElementReferenceException
。下面是完整的代码以及到源代码的链接
ExpectedConditions.presenceOfAllElementsLocatedBy(locator)
//
///检查网页上存在的所有元素
///匹配定位器。
///
///用于查找元素的定位器。
///一旦找到它,它的列表。
由(定位器)定位的所有元素的公共静态函数存在
{
返回(驱动程序)=>
{
尝试
{
var元素=driver.FindElements(定位器);
返回元素。Any()?元素:null;
}
捕获(StaleElementReferenceException)
{
返回null;
}
};
}
关键点:使用给定的By机制查找当前页面中的所有元素 借助定位器值定位文档中的元素 当隐式等待时,只要找到的集合中有0个以上的项,此方法就会返回,或者如果达到超时,则返回空列表
/// <summary>
/// An expectation for checking that all elements present on the web page that
/// match the locator.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns>The list of <see cref="IWebElement"/> once it is located.</returns>
public static Func<IWebDriver, ReadOnlyCollection<IWebElement>> PresenceOfAllElementsLocatedBy(By locator)
{
return (driver) =>
{
try
{
var elements = driver.FindElements(locator);
return elements.Any() ? elements : null;
}
catch (StaleElementReferenceException)
{
return null;
}
};
}