簡體   English   中英

System.Reflection.TargetInvocationException :調用的目標已拋出異常

[英]System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation

我正在嘗試自動化 reactjs 應用程序和我們的項目使用的框架,該框架基於 C# 和量角器網絡構建。

在任何單擊或斷言函數后,我收到以下錯誤,但代碼中定義的操作成功執行。

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> OpenQA.Selenium.WebDriverTimeoutException : timeout

這個錯誤的原因是什么?

    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Interactions;
    using OpenQA.Selenium.Support.PageObjects;
    using OpenQA.Selenium.Support.UI;
    using Protractor;
    using System;
    using System.Collections.Generic;


    public Class personalinformations
    {

    private NgWebDriver _ngdriver;


            public PersonalInformations(IWebDriver driver)
            {

                _ngdriver = new NgWebDriver(driver);
                PageFactory.InitElements(_ngdriver, this);
                _ngdriver.IgnoreSynchronization = true;

            }

     [FindsBy(How = How.Id, Using = "btnSubmit")]
            private IWebElement btnsave { get; set; }

     public void saveSection()
            {
WebDriverWait wait = new WebDriverWait(ngdriver, TimeSpan.FromSeconds(30));         
           wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*@id='btnSubmit']"));

btnsave.Click();
    }
}

注意:雖然使用 Thread.Sleep(1000) 等待有時代碼有效。此外,我嘗試使用 Javascript 單擊元素,結果是相同的。

通過WebDriverWaitExpectedConditions方法ElementIsVisible等待元素后,您將在下一步調用Click()因此您需要調用ElementToBeClickable方法而不是ElementIsVisible方法,如下所示:

public void saveSection()
{
    WebDriverWait wait = new WebDriverWait(ngdriver, TimeSpan.FromSeconds(30));         
    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*@id='btnSubmit']"));
    btnsave.Click();
}

這是一個有趣的異常:“System.Reflection.TargetInvocationException:調用的目標已拋出異常。”; 我遇到了好幾次,但我搜索了這篇文章“調用的目標拋出了異常”錯誤(mscorlib),他們說你應該檢查這個異常背后的根本原因。 所以我加了一個

嘗試 {element.Click();} catch(Exception e){Console.WriteLine(e);}

然后異常似乎逃脫了......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM