System.Reflection.TargetInvocationException :调用的目标已抛出异常 [英] System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation

查看:61
本文介绍了System.Reflection.TargetInvocationException :调用的目标已抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动化 reactjs 应用程序和我们的项目使用的框架,该框架基于 C# 和量角器网络构建.

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

注意:虽然使用 Thread.Sleep(1000) 等待有时代码有效.我也尝试使用 Javascript 单击元素,结果是相同的.

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.

推荐答案

一旦你通过WebDriverWaitExpectedConditions方法等待元素ElementIsVisible在下一步中,您将调用 Click(),因此您需要调用 ElementToBeClickable 方法如下:

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

这篇关于System.Reflection.TargetInvocationException :调用的目标已抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆