我正在尝试扩展IWebElement
C# 中的接口以添加一种新方法来防止StaleElementReferenceException
.
我要添加的方法很简单retryingClick
,它将尝试在放弃之前最多单击 WebElement 三次:
public static void retryingClick(this IWebElement element)
{
int attempts = 0;
while (attempts <= 2)
{
try
{
element.Click();
}
catch (StaleElementReferenceException)
{
attempts++;
}
}
}
添加该方法的原因是我们的网页大量使用了jQuery,并且很多元素是动态创建/销毁的,因此为每个元素添加保护WebElement
成为一项巨大的考验。
所以问题就变成了:我应该如何实现这个方法,以便接口IWebElement
可以随时使用它?
谢谢,问候。