webdriver-实用程序/帮助程序方法-等待类名包含特定的样式属性
本文关键字:包含特 属性 样式 等待 实用程序 帮助程序 方法 webdriver- | 更新日期: 2023-09-27 18:26:30
我有一个Web应用程序,它将包含一个"Loading"类,当它完全加载到页面上时,它将含有100%的宽度属性,否则它将不包含任何内容。我正在尝试对此样式属性执行检查,但一直超时。我正在做的是:
我从一个helper/utility类中调用代码,如下所示,因为这是我将在多个类中经常使用的东西:
Utility.WaitForStyle("Loading", Utility.driver);
在我的助手/实用程序类中,我有以下代码:
public static void WaitForStyle(string Class, IWebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until<bool>((d) =>
{
try
{
IWebElement element = d.FindElement(By.ClassName(Class));
String elementresults = element.GetAttribute("style");
System.Diagnostics.Debug.WriteLine(elementresults);
return false;
}
catch (NoSuchElementException)
{
return true;
}
});
}
注意,上面的代码目前只是想检查它是否可以获得类的style属性的句柄,但它还没有达到这一点。我知道问题出在实用程序方法中,因为我可以在单独的类中使用以下代码:
IWebElement element = Utility.driver.FindElement(By.ClassName("Loading"));
String elementresults = element.GetAttribute("style");
System.Diagnostics.Debug.WriteLine(elementresults);
这将按预期打印出"宽度:100%",所以我知道这段代码实际上工作正常
有人知道我的实用方法是否在做一些愚蠢的事情吗?
下面是我等待元素属性具有特定值的代码。它假设传递给它的元素已被验证存在:
public bool WaitForAttribute(IWebDriver driver, IWebElement element, string attributeName, string attributeValue, int timeOut = 5)
{
// Build a function with a signature compatible with the WebDriverWait.Until method
Func<IWebDriver, bool> testCondition = (x) => element.GetAttribute(attributeName).Equals(attributeValue);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut));
// Wait until either the test condition is true or timeout
try { wait.Until(testCondition); }
catch (WebDriverTimeoutException e) { }
// Return a value to indicate if our wait was successful
return testCondition.Invoke(null);
}
4个多月以来,这对我来说一直有效。
public static WebDriverWait wait = new WebDriverWait(SeleniumInfo.Driver, TimeSpan.FromSeconds(20));
public static void WaitUntilAttributeValueEquals(this IWebElement webElement, String attributeName, String attributeValue)
{
wait.Until<IWebElement>((d) =>
{
if (webElement.GetAttribute(attributeName) == attributeValue)
{
return webElement;
}
return null;
});
}