【发布时间】:2020-10-27 08:59:12
【问题描述】:
我正在尝试添加方法,该方法将使用显式等待来等待元素不可见:
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" +
"______________________________________________________");
}
}
测试页面使用反应,在页面操作之间(在页面之间切换)有一个加载器图标,持续时间少于几秒到 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>
当加载器消失时,只有一个空的跨度部分:
无论我使用什么定位器:
public static void WaitForInnerLoaderDisappear(this IWebDriver driver, int timeoutInSeconds = 30)
{
driver.WaitForElementNotVisible(By.XPath("//div[@class = 'Loader__background']"), timeoutInSeconds);
}
等待没有按应有的方式工作。实际上它会等待并点击页面元素,但它的速度非常慢。加载程序不再“眼睛”可见”,而是在两次点击之间等待 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执行器混合是否正确?它运行得更快,但仍然出现片状,所以我认为有些地方不正确。
【问题讨论】:
-
如果有帮助,请在我的答案中添加计时器版本
标签: c# selenium selenium-webdriver dom