0

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rajz
  • 21
  • 1
  • 3
  • Welcome to Stack Overflow! Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error and the shortest code necessary* to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [mcve]. – JeffC Apr 30 '18 at 00:06
  • Code trials please – undetected Selenium Apr 30 '18 at 06:17
  • updated the code please – Rajz Apr 30 '18 at 14:01

2 Answers2

2

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();
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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...