C# 等待加载程序的Selenium消失
我正在尝试添加一个方法,该方法将使用显式等待来等待元素不可见:C# 等待加载程序的Selenium消失,c#,selenium,selenium-webdriver,dom,C#,Selenium,Selenium Webdriver,Dom,我正在尝试添加一个方法,该方法将使用显式等待来等待元素不可见: public static void WaitForElementNotVisible(this IWebDriver driver, By by, int timeoutInSeconds = 5) { try { WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeo
public static void WaitForElementNotVisible(this IWebDriver driver, By by, int timeoutInSeconds = 5)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(by));
}
catch (WebDriverTimeoutException)
{
throw new WebDriverTimeoutException("\n" +
"**************************************\n" +
"Below element should be not visible:\n" +
"**************************************\n" +
by.ToString() + " \n" +
"Timeout after: " + timeoutInSeconds.ToString() + " seconds\n" +
"______________________________________________________");
}
}
测试页面使用react,在页面操作之间(在页面之间切换)有一个加载程序图标,根据操作的不同,该图标的持续时间从少于秒到3/4秒不等
该加载程序的DOM如下所示:
<span>
<div class="Loader__background" style="display: block; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-color: unset; z-index: 10;">
<div class="Loader__foreground" style="display: table; width: 100%; height: 100%; text-align: center; z-index: 20; color: white;">
<div class="Loader__message" style="display: table-cell; vertical-align: middle;">
<div class="sc-fzqBkg jCOtDD">
<div class="sc-fzqPZZ kyyPPI"></div>
</div>
</div>
</div>
</div>
</span>
等待并没有以它应该的方式起作用。实际上,它会等待并点击页面元素,但速度非常慢。加载器不再“眼睛”可见,而是在点击之间等待10到40秒。似乎某种状态仍然可见。问题可能出在哪里
编辑
正如@Dazed所说,我现在已经做了一个更改,我正在检查集合的大小
public static void WaitForElemNotDisplayed_byXPath(this IWebDriver driver, int timeoutInSeconds = 30)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
int loaderAmount = int.Parse(js.ExecuteScript($"return document.querySelectorAll(\"div[class*='Loader_background']\").length;").ToString());
wait.Until(webDriver => loaderAmount == 0);
}
catch (Exception) { throw; }
}
将wait.Untill与Javascript executor混合使用是否正确?它运行得更快,但仍然会出现片状,因此我认为有些地方不正确。我所做的一件事是在加载元素时以及元素消失后拍摄元素的屏幕截图。这有助于我识别不同之处。很多时候,这只是一个样式更改 您可以尝试下面的方法并传入xpath
WaitForElemNotDisplayed_byXPath("//div[@class='myEle']");
public static void WaitForElemNotDisplayed_byXPath(string elementClass)
{
try
{
_wait.Until(webDriver => driver.FindElements(By.XPath(elementClass)).Count == 0);
}
catch (Exception) { throw; }
}
您还可以向其添加计时器:
public static void WaitForElementNoLongerDisplayed_byXPathTime(string value)
{
try
{
var wait = new DriverWait(driver, new TimeSpan(0, 0, 40));
wait.Until(webDriver => Driver.FindElements(By.XPath(value)).Count == 0);
}
catch (Exception) { throw; }
}
参考:在我的答案中添加了一个计时器版本,如果它有帮助的话谢谢你的答案,事实上我是按照你发布的方式做的,但结果是它可以工作,但速度非常慢。我做了一些更改,而不是使用JavaScript Executor尝试使用FindElements,它应该更快,但我不确定代码是否正确。你能看看我的ed吗有问题吗?
public static void WaitForElementNoLongerDisplayed_byXPathTime(string value)
{
try
{
var wait = new DriverWait(driver, new TimeSpan(0, 0, 40));
wait.Until(webDriver => Driver.FindElements(By.XPath(value)).Count == 0);
}
catch (Exception) { throw; }
}