C# Selenium:等待进度条过程在UI中完成(等待进度条可能会因测试而异)
我正在使用selenium进行UI自动化,我希望等待进度条完成,在进度条完成后,我希望执行一些其他操作。我已经尝试过了,等等,元素的隐形定位,但它不起作用。请建议一些使用C#Selenium处理进度条的方法 我尝试了等待,直到特定Xpath的元素定位函数不可见C# Selenium:等待进度条过程在UI中完成(等待进度条可能会因测试而异),c#,selenium,automation,progress-bar,webautomation,C#,Selenium,Automation,Progress Bar,Webautomation,我正在使用selenium进行UI自动化,我希望等待进度条完成,在进度条完成后,我希望执行一些其他操作。我已经尝试过了,等等,元素的隐形定位,但它不起作用。请建议一些使用C#Selenium处理进度条的方法 我尝试了等待,直到特定Xpath的元素定位函数不可见 WebDriverWait wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(900000000));
WebDriverWait wait = new WebDriverWait(driver, System.TimeSpan.FromSeconds(900000000));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath(buildProgressStringExp)));
string buildProgressStringExp = "//table[@class='pane stripped']/tbody//descendant::tr[@class='build-row transitive single-line overflow-checked']//table[@class='progress-bar ']"
我尝试了下面的代码,但仍然不起作用。它一直等到特定的时间限制,然后抛出异常
{while(true)
{
try
{
IWebElement el = driver.FindElement(By.XPath("Xpath of bar"));
bool abc = el.GetAttribute("style").Equals("width:100%;");
if (abc)
{
return true;
}
}
catch(StaleElementReferenceException exp)
{
return true;
}
Thread.Sleep(1000);
}
}```
可能是元素不符合该预期条件所使用的标准。下面是代码
//
///检查元素在DOM中是否不可见或不存在的期望。
///
///用于查找元素的定位器。
///如果未显示该元素;否则。
公共静态函数InvisibilityFelementLocated(通过定位器)
{
返回(驱动程序)=>
{
尝试
{
var元素=驱动程序FindElement(定位器);
返回!元素。已显示;
}
捕获(无接触元素异常)
{
//返回true,因为元素不在DOM中
//尝试块检查元素是否存在但不可见。
返回true;
}
捕获(StaleElementReferenceException)
{
//返回true,因为过时的元素引用暗示该元素
//不再可见。
返回true;
}
};
}
如果页面没有改变,它将不会过时,如果搜索找到它,但它是某种隐藏的另一种方式,显示永远不会返回false,这将永远不会起作用
您可能需要编写自己的func才能使用您的页面。在构建结束时,屏幕上的某些状态将发生变化,例如小圆圈将变为红色或绿色,对吗?或者“x”图标将消失,您是否尝试使用它们作为等待的基础?请花一分钟时间修复代码上的缩进。另外,请阅读原因。粘贴代码并正确格式化。页面的截图很好,但HTML的截图不好。你知道你等待的时间是28.5年,对吗?这似乎有点傻,不是吗?@Spencer,在构建结束时,第一个元素的xpath发生了变化,x图标也消失了。我两个都试过了,但仍然不起作用。@Jeff我会这样做的,因为我建议坦克Cain响应,但这些是内置函数,有没有办法更新它?@Zoso619我不确定我是否遵循。InvisibilityOfElementLocated是一个现有的方法,但您可以为编写自己的函数,直到使用为止,该函数是特定于所需条件的。也可以在任何时候使用Wait.Until设置一个最大时间,在该时间之后将引发异常。如果进度条花费的时间超过x,则等待失败。尝试此操作后,它对我无效。实际上,驱动程序在抛出陈旧元素异常之前要等待5-6分钟。不知道为什么它不等待,直到酒吧在用户界面中不可见。等待时间不超过5-6分钟。对于持续时间较长的测试用例,测试失败。
/// <summary>
/// An expectation for checking that an element is either invisible or not present on the DOM.
/// </summary>
/// <param name="locator">The locator used to find the element.</param>
/// <returns><see langword="true"/> if the element is not displayed; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> InvisibilityOfElementLocated(By locator)
{
return (driver) =>
{
try
{
var element = driver.FindElement(locator);
return !element.Displayed;
}
catch (NoSuchElementException)
{
// Returns true because the element is not present in DOM. The
// try block checks if the element is present but is invisible.
return true;
}
catch (StaleElementReferenceException)
{
// Returns true because stale element reference implies that element
// is no longer visible.
return true;
}
};
}