【问题标题】:Selenium c# : How to catch exception from function in WebDriverWait().Until() [duplicate]Selenium c#:如何从 WebDriverWait().Until() 中的函数中捕获异常 [重复]
【发布时间】:2020-11-12 03:57:35
【问题描述】:

我使用以下函数等待页面完全加载后再处理:

protected bool WaitForPageLoad(){
    return new WebDriverWait(Driver, TimeSpan.FromSeconds(20)).Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
}

有时IJavaScriptExecutor.ExecuteScript() 返回一个OpenQA.Selenium.NoSuchWindowException,尽管我将WaitForPageLoad() 调用包含在try catch 块中,但仍然没有捕获到该异常。

如何捕捉它?

【问题讨论】:

    标签: c# selenium selenium-webdriver exception webdriverwait


    【解决方案1】:

    您可以将 webdriverwait 配置为忽略某些异常。

    你可以这样使用它:

    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(NoSuchWindowException));
    wait.Until(d=> ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多