简体   繁体   English

System.Reflection.TargetInvocationException :调用的目标已抛出异常

[英]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.我正在尝试自动化 reactjs 应用程序和我们的项目使用的框架,该框架基于 C# 和量角器网络构建。

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.注意:虽然使用 Thread.Sleep(1000) 等待有时代码有效。此外,我尝试使用 Javascript 单击元素,结果是相同的。

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

this is a funny Exception: "System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.";这是一个有趣的异常:“System.Reflection.TargetInvocationException:调用的目标已抛出异常。”; 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.我遇到了好几次,但我搜索了这篇文章“调用的目标抛出了异常”错误(mscorlib),他们说你应该检查这个异常背后的根本原因。 so i added a所以我加了一个

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

then the exception seems escaped away...然后异常似乎逃脱了......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Reflection.TargetInvocationException: '调用的目标已抛出异常 - System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException:调用的目标已引发异常 - System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation 为什么我收到异常 System.Reflection.TargetInvocationException: 'Exception has been throwed by the target of an invocation.'? - Why I'm getting exception System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'? Xamarin Forms System.Reflection.TargetInvocationException: '调用的目标已抛出异常。' - Xamarin Forms System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' Xamarin.form-Masterdetailpage:System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin.form - Masterdetailpage : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin 表单:System.Reflection.TargetInvocationException:调用的目标已抛出异常 - Xamarin Forms: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation 使用 Task.Run 时如何修复“System.Reflection.TargetInvocationException: 'Exception has been throwed by the target of an invocation.'” - How to fix “System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'” when use Task.Run 如何修复“System.Reflection.TargetInvocationException已被抛出” - How to fix “System.Reflection.TargetInvocationException has been thrown” TargetInvocationException:调用的目标引发了异常。 关于数据绑定 - TargetInvocationException: Exception has been thrown by the target of an invocation. on Data Bind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM