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