0

I use C# with Selenium and testing the application running on Chrome at the moment but hoping to expand all browsers. So I try couple code below and they're not working on the click action. I use XPath, but it throws an exception saying there is no element found in the form. I don't put submit on the form. I also use the other normal way, but it does not submit anything:

    <div id="textAreaSection">
        <div class="textArea">
            <label><b>TextArea:</b></label><br>
            <textarea id="textAreaText" rows="14" cols="40"></textarea>
        </div>
        <div class="surveyBtn">
            <input id="inputSubmit" onClick="inputText()" type="submit" value="Input Text">
        </div>
   </div>

I try this, but no clicking or submitting anything after it's executed:

IWebElement tagElement = driver.FindElement(By.Id("inputSubmit"));
tagElement.Submit();

I try XPath, but the exception is thrown saying that it cannot find an element in the form:

driver.FindElement(By.XPath("//input[@id='inputSubmit']")).Submit();

Update: 1

I try to use WebDriverWait suggested by LoflinA but still throws an exception about not clickable at point (387,590). Another element would receive the click:

public void WaitUntilClickable(IWebElement elementLocator, int timeout)
{
    try
    {
        WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
        waitForElement.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
    }
    catch (NoSuchElementException)
    {
        Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
        throw;
    }
}

And here is the caller block:

    IWebElement tag = driver.FindElement(By.XPath("//div[@class='surveyBtn']/input[@id='inputSubmit']"));

    WaitUntilClickable(tag, 10);

    tag.Click();

Update: 2 Thanks to @chris-crush-code below code works!

 public void CallSubmitType(IWebElement tag, IWebDriver driver)
    {
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        string script = tag.GetAttribute("onClick");
        js.ExecuteScript(script);
    }

    [Then(@"SpecFlowTesting")]
    public void SpecFlowTesting(string expectedStr)
    {                        
        IWebElement tag = driver.FindElement(By.Id("inputSubmit"));
        CallSubmitType(tag, driver);

        IWebElement tagTextArea = webDriver.FindElement(By.Id("textAreaText"));
        string txt = tagTextArea.GetAttribute("value");
        Assert.AreEqual(expectedStr, txt);
    }
Phil
  • 339
  • 2
  • 13
  • Did you try Click()-method? – Ratmir Asanov Jan 22 '18 at 17:42
  • i just try with click() and the exception says "unknown error: Element is not clickable at point (387, 590). Other element would receive the click:
    ...
    – Phil Jan 22 '18 at 17:55
  • Possible duplicate of [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – undetected Selenium Jan 22 '18 at 18:01
  • when I searched on google, i didn't enounter this post. I will take a look although I use SpecFlow C# with Selenium. – Phil Jan 22 '18 at 18:30

3 Answers3

1

As you have mentioned receiving the error detailing that another element would receive the click, I am thinking that your page has not loaded completely prior to the action being attempted. Try the following to wait for the element to be clickable:

    // Wait Until Object is Clickable
    public static void WaitUntilClickable(IWebElement elementLocator, int timeout)
    {
        try
        {
            WebDriverWait waitForElement = new WebDriverWait(DriverUtil.driver, TimeSpan.FromSeconds(10));
            waitForElement.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
    }

Also see: Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:

LoflinA
  • 350
  • 4
  • 19
  • thank you for your depth analyst on the code. I will try your code and let you know. – Phil Jan 22 '18 at 18:27
  • Is there some sort of overlay being placed on the page at load? Is there a new frame being created that needs to be switched to? I have experienced needing to click somewhere on the pageBody in order to allow clicking on any other element. Have you tried implementing a click anywhere else on this same page prior to this one being clicked? – LoflinA Jan 22 '18 at 18:47
  • This inputSubmit is on the main page. there is no navigator or sidbar. Just plain with
    , , , are just simple elements as i try to learn web and SpecFlow Selenium. Although there is other click on the hyper link but that is in different test case so it shouldn't be executed since I only focus on this specific test case for dealing with inputSubmit. Also every time when the tested test case completes, the web driver will be closed.
    – Phil Jan 22 '18 at 18:57
1

a recent update of chrome requires you to scroll to the clickable object. So:

var js = (IJavascriptExecutor)driver;
IWebElement obj = driver.FindElement(By.XPath("//input[@id='inputSubmit']"));
js.ExecuteScript("arguments[0].scrollIntoView(true);", obj);

obj.Click();
chris-crush-code
  • 1,114
  • 2
  • 8
  • 17
0

As per the HTML you have shared the WebElement clearly have a onClick() attribute so Java click() should work. But we are not sure if the WebElement is within a form or not to guarantee submit() method to work.

To click() on the button with value as Input Text you can try the following line of code :

driver.FindElement(By.XPath("//div[@class='surveyBtn']/input[@id='inputSubmit']")).Click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I just try, but threw exception. – Phil Jan 22 '18 at 17:56
  • I try with your latest updates, but the same exception. That element with Id inputSubmit is not in form tag. Sorry I'm pretty new web so i was aware about using submit with form. "An exception of type 'System.InvalidOperationException' occurred in WebDriver.dll but was not handled in user code Additional information: unknown error: Element is not clickable at point (387, 590). Other element would receive the click:
    ...
    "
    – Phil Jan 22 '18 at 18:03
  • I meant, I was not aware of using submit with form. – Phil Jan 22 '18 at 18:13