C# .NET硒元素异常;WebDriverWait.Until()不';行不通
我们有一个.NET4.5、MVC、C#项目。我们正在使用Selenium进行UI测试,测试在我们使用的C# .NET硒元素异常;WebDriverWait.Until()不';行不通,c#,.net,selenium,nosuchelementexception,C#,.net,Selenium,Nosuchelementexception,我们有一个.NET4.5、MVC、C#项目。我们正在使用Selenium进行UI测试,测试在我们使用的wait.Until()行上会间歇性失败。其中一个例子是: NoSuchElementException was unhandled by user code An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user cod
wait.Until()
行上会间歇性失败。其中一个例子是:
NoSuchElementException was unhandled by user code
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}
它就扔在这里:
Thread.Sleep(600);
wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));
我可以看到浏览器打开,我看到它到达那个点,我可以检查元素以查看元素是否存在。它的id完全正确
我们有很多这样的问题,到目前为止,解决方法是在它前面加上Thread.Sleep(600)(或者类似的时间)。wait.Until()
的全部目的就是不必这样做,这使得我们的测试套件变得很长。此外,正如您在上面的示例中所看到的,有时即使在前面放置了Thread.Sleep()
,我们也会遇到问题,因此我们必须延长时间
为什么.NET Selenium的WebDriver.Until()
不起作用,有没有其他方法可以不用等待一段时间就完成同样的事情?同样,这个问题是间歇性的,这意味着它只会偶尔发生,并且它可能发生在任何数量的wait.Until()
语句中,而不仅仅是显示的语句
编辑:
这是一个类变量。
私有WebDriverWait等待代码>
它是这样实例化的:
this.wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
我决定不使用WebDriverWait.Until()
,而是在主驱动程序上使用隐式等待:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
我完全相信他在另一个问题上给了我这个想法
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
try
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
catch
{
throw;
}
}
或者如果你有
非接触性异常
试试这个代码
try
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds)).Until(ExpectedConditions.ElementIsVisible(by));
}
return driver.FindElement(by);
}
catch(Exception e)
{
throw;
}
如何定义等待
?谢谢。@alecx请参见上面的编辑。很抱歉,我忘了提到这一点。@alecxe我正在这么做……请再次参阅编辑;再一次抱歉,它真的要等一段时间吗?