【发布时间】:2019-04-04 11:06:23
【问题描述】:
我正在尝试运行 Selenium 测试,它会等到某个属性具有值才能继续,并且在浏览器中检查时(代码检查 Ctrl+shift +I, ctrl+F),它使用css定位器定位元素,但我在运行测试时,总是得到一个timeout - 并且元素在该时间之前获得所需的值
- 检查 css 定位器在“检查代码”和搜索中是否有效
- 尝试了更简单的定位器 - 它可以定位 id,但无法定位属性及其值
- 疯狂地谷歌搜索以找到解决方案
我的代码(CMExtensionMethods.IsElementVisible == ExpectedConditions,做同样的事情):
public static WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
waitForElement.Until(CMExtensionMethods.IsElementVisible(By.CssSelector("#page_progress[style~='display:']")));
public static Func<IWebDriver, IWebElement> IsElementVisible(By identifier) {
return (driver) =>
{
try {
return IfElementVisible(driver.FindElement(identifier));
}
catch(NoSuchElementException) {
return null;
}
};
}
// Part of the method above
private static IWebElement IfElementVisible(IWebElement element) {
return element.Displayed ? element : null;
}
页面上的 HTML 代码(page_progress 样式根据网站状态而变化 - 样式可以是这样的,也可以是空的):
<div id="page_progress" class="loading-progress" style="display: none;">
</div>
<div id="page_saveindicator" class="unsaved-changes" style="display:
none;"></div>
期望:在样式获得值后立即继续的代码
结果:没有找到任何东西,并且我在 5 秒(按设置)后收到超时错误,尽管元素具有属性和它应该具有的值
可能值得一提 - IsElementVisible 方法有效 - 使用 Class 和 Id(单独)在 ID 和 CSS 选择器上进行测试
【问题讨论】: