简体   繁体   中英

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

I am trying to automate a reactjs application and the framework our project is using built on C# and protractor-net.

After any click or assert function I get the following error, but the defined action in the code executes successfully.

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

What is the cause of this error?

    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();
    }
}

Note: While using Thread.Sleep(1000) for wait sometimes the code works.Also I tried with Javascript to click the element the result is same.

Once you wait for the element through WebDriverWait and ExpectedConditions method ElementIsVisible as in the next step you are invoking Click() so instead of ElementIsVisible method you need to invoke ElementToBeClickable method as follows :

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

this is a funny Exception: "System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation."; i met this for several times, but i searched this post "Exception has been thrown by the target of an invocation" error (mscorlib) they said that you should check the root cause of this Exception behind. so i added a

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

then the exception seems escaped away...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM