OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError at WebElement Submit click - c#

I'm trying to automate a scenario for some web pages using C#, Selenium and Chrome Webdriver, where Submit button click on the page will submit the sendkeys values.
however when Submit is clicked, it's throwing an UnpackAndThrowOnError error in Visual Studio Test explorer Window (Nunit).
<button type="submit" class="btn btn-primary mr-4" name="postType" value="Submit">Submit<span class="glyphicon glyphicon-floppy-save"></span></button>
Detailed Error Description:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(ResponseerrorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary'2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary'2 parameters)<br>
at OpenQA.Selenium.Remote.RemoteWebElement.Click()
at Onrsr.Specflow.Sampletest.occurrence() in C:\Users\manish.sharma\source\repos\Onrsr.Specflow\Onrsr.Specflow\Sampletest.cs:line 180
I've tried the below code options, but they all are failing at submitBtnReview.Click()
IWebElement submitBtnReview = driver.FindElement(By.XPath("//button[contains(.,'Submit')]"));
submitBtnReview.Click();
IWebElement submitBtnReview = driver.FindElement(By.CssSelector("input[value='Submit']"));
submitBtnReview.Click();
IWebElement submitBtnReview = driver.FindElement(By.XPath("//button[#class='btn btn-primary mr-4']"));
submitBtnReview.Click();
IWebElement submitBtnReview = driver.FindElement(By.CssSelector("input[type='submit'][value='Submit']"));
submitBtnReview.Click();
I've also tried using submitBtnReview.Submit() with the IwebElement above, however it's crashing the page.
I'm using latest version of Selenium.WebDriver (3.141.0) and Selenium.Chrome.WebDriver (2.43.0), and using chrome version 70.0.3538.102 (Official Build) (64-bit) on windows 10 machine.
Any idea what I might be doing wrong here?
[05/12/2018] - thank you for all valuable feedbacks - I've now also tried the below and they are failing at submitBtnReview.Click() with a new error:
Message: OpenQA.Selenium.WebDriverException : unknown error: Element <button type="submit" class="btn btn-primary mr-4" name="postType" value="Submit">...</button> is not clickable at point (1792, 876). Other element would receive the click: <footer class="border-top bg-white">...</footer>
(Session info: chrome=70.0.3538.110)
(Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64)
IWebElement submitBtnReview = driver.FindElement(By.CssSelector("button[type='submit'][value='Submit'][name='postType']"));
IWebElement submitBtnReview = driver.FindElement(By.XPath("//button[contains(#class, 'btn') and contains(#class, 'btn-primary') and contains(#class, 'mr-4')]"));
IWebElement submitBtnReview = driver.FindElement(By.XPath("//button[#class='btn btn-primary mr-4']"));
IWebElement submitBtnReview = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[#class='btn btn-primary mr-4' and #name='postType'][normalize-space()='Submit']")));
IWebElement submitBtnReview = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(element_is_present(By.CssSelector("button[type='submit'][value='Submit'][name='postType']")))
public static Func<IWebDriver, IWebElement> element_is_present(By by)
{ return driver =>
{ IWebElement element = driver.FindElement(by);
try
{ if (element != null && element.Displayed && element.Enabled)
{ return element;
}
else
{ return null;
}
}catch (StaleElementReferenceException)
{ return null;
}
};
}
Setting some breakpoints, showing submitBtnReview LocationOnScreen coordinates with a new error, and this might be related.
LocationOnScreen = '((OpenQA.Selenium.Remote.RemoteCoordinates)((OpenQA.Selenium.Remote.RemoteWebElement)submitBtnReview ).Coordinates).LocationOnScreen' threw an exception of type 'System.NotImplementedException'
Submit button details from debugging:
Error image:
Hope this additional information help finding the cause - thanks in advance!!

No exact answer so far, just some observations. 3 of given locators look incorrect, although 1 looks ok.
1) By.XPath("//button[contains(.,'Submit')]")); ---> Looks correct --- explained here
Suggestion: if text value is modified by any of given element class values and a button is shown on a screen as 'SUBMIT' (all caps) or submit (all lowercase) you should use it in xpath as well to be "//button[contains(.,'SUBMIT')]" or "//button[contains(.,'submit')]" .
2) By.CssSelector("input[value='Submit']")); ---> By.CssSelector("button[value='Submit']")); --- your tag is <button> not <input>
3) By.XPath("//button[#class='btn btn-primary mr-4']"); ---> By.XPath("//button[contains(#class, 'btn') and contains(#class, 'btn-primary') and contains(#class, 'mr-4')]) --- explained here
4) By.CssSelector("input[type='submit'][value='Submit']")); ---> By.CssSelector("button[type='submit'][value='Submit']")); --- your tag is <button> not <input> explained here
Before clicking, make sure the element is clickable
public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
return driver =>
{
var element = driver.FindElement(locator);
return (element != null && element.Displayed && element.Enabled) ? element : null;
};
}
Usable in something like:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id"))
original source
[Addition after question as edited]
The error states that you have an element and that selenium is not able to click on it at the point. the problem is described and solved here
In my opinion, the most stable yet the hackiest way is to try out with the javascript click (it just have to click if you have an element on the screen, and based on the examples you do have it):
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);
Another case is to make sure your element is located in the visible area of a browser (you can see/click it during debug without any manual scrolling). If it's not visible or not entirely visible - scroll to it before clicking.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView()", Webelement);
Double check suggestion: make sure that there is exactly the one element with such locator. The usual case is when you think that you found 'the right' element but selenium founds the first-match and it is not the one you want.
Another suggestion is that your element might change it's position after you found it. So you found it, saved to a variable and then for some reason the page layout changed (e.g. something async load finished). You can carefully pay attention to how your page loads. Does layout change? If yes - you can create a custom wait function that will check that element coordinates does not change for, let's say, 1-2 seconds.

The element looks to be a dynamic element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.btn.btn-primary.mr-4[name='postType'][value='Submit']"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[#class='btn btn-primary mr-4' and #name='postType'][normalize-space()='Submit']"))).Click();
Note: As you are using ChromeDriver v2.43 ensure that you are using Chrome v69-71

Related

Element not interactable with C# app that uses Chrome WebDriver

PREFACE: After a lengthy Stack Overflow search I found two suggested solutions to solve the "element not interactable" problem I am having when I try to interact with the target node element. Neither of them worked, as described below.
I have a C# app that uses the OpenQA.Selenium package to remote control a YouTube web page. I am trying to click on a button on the page that opens a dialog box, but when I do I get the notorious "element not interactable" message. I found the following two suggestions on Stack Overflow:
Actions actions = new Actions(chromeDriver);
actions.MoveToElement(webElem);
actions.Perform();
And this suggestion that one commenter said is ill-advised because it can click on elements that are not visible or are below modal objects:
IJavaScriptExecutor executor = (IJavaScriptExecutor)chromeDriver;
executor.ExecuteScript("arguments[0].click();", webElem);
I tried the second one anyways to see if it worked. Unfortunately, with the first suggestion that uses the Actions interface, I still got "element not interactable" message but this time on the Perform() statement. The third attempt did not get the error message but it failed to click the button. I know this because clicking the button opens a dialog window when it works, and no dialog window appeared when I tried the third solution.
Below is the code I am using to try and click on the element. The collection it iterates are the elements I select via an XPath statement that finds the button I am want to click. It tries every button that matches the XPath statement and skips those that fail to work. Unfortunately, none of the 3 buttons found by the XPath statement work.
What is strange is that if I take the exact same XPath statement I am using in my C# app and plug it into the Chrome DevTools debugger, referencing the first element in the array of found elements, it works:
$x(strXPath)[0].click()
But so far nothing I have tried from C# app works. Does anyone have an idea on why I am having this problem?
public IWebElement ClickFirstInteractable(ChromeDriver chromeDriver)
{
string errPrefix = "(ClickFirstInteractable) ";
if (this.DOM_WebElemensFound == null || this.DOM_WebElemensFound.Count() < 1)
throw new NullReferenceException(errPrefix + "The DOM_WebElementsFound collection is empty.");
IWebElement webElemClicked = null;
foreach (IWebElement webElem in this.DOM_WebElemensFound)
{
// Try and "click" it.
try
{
// First make sure the element is visible, or we will get
// the "element not interactable" error.
/* FIRST ATTEMPT, didn't work.
*
webElem.scrollIntoView(true);
webElem.Click(); // <<<<<----- Error occurs here
*/
/* SECOND ATTEMPT using Actions, didn't work
* and I go the error message when the Perform() statement executes.
Actions actions = new Actions(chromeDriver);
actions.MoveToElement(webElem);
actions.Perform(); // <<<<<----- Error occurs here
*/
/* THIRD ATTEMPT using script execution, didn't work.
* I did not get the error message, but the button did not get clicked.
*/
IJavaScriptExecutor executor = (IJavaScriptExecutor)chromeDriver;
executor.ExecuteScript("arguments[0].scrollIntoView();", webElem);
executor.ExecuteScript("arguments[0].click();", webElem);
// Click operation accepted. Stop iteration.
webElemClicked = webElem;
break;
}
catch (ElementNotInteractableException exc)
{
// Swallow this exception and go on to the next element found by the XPath expression.
System.Console.WriteLine(exc.Message);
}
}
return webElemClicked;
}
I tried to reproduce your scenario by clicking on a "hidden" button, waiting for the modal to appear, then acting on that modal, etc.
I hope it helps you!
const string Target = #"https://www.youtube.com/";
using var driver = new ChromeDriver();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20))
{
PollingInterval = TimeSpan.FromMilliseconds(250),
};
driver.Navigate().GoToUrl(Target);
// i don't consent cookies to
// save time, so just do it
// here manually and then press enter to console
Console.ReadLine();
var menuLocator = By.XPath("//a[#id = 'video-title-link'][1]" +
"/ancestor::div[#id = 'meta']" +
"/following-sibling::div[#id = 'menu']" +
"//button[#class = 'style-scope yt-icon-button']");
var menu = wait.Until(d => d.FindElement(menuLocator));
var actions = new Actions(driver);
actions.MoveToElement(menu).Click().Perform();
var shareLocator = By.XPath("//div[#id = 'contentWrapper']//*[normalize-space(text()) = 'Share']");
var share = wait.Until(d => d.FindElement(shareLocator));
actions.MoveToElement(share).Click().Perform();
var copyLinkLocator = By.XPath("//button[#aria-label = 'Copy']");
var copyLink = wait.Until(d => d.FindElement(copyLinkLocator));
actions.MoveToElement(copyLink).Click().Perform();

C#+ Selenium: click on submit

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

Selenium Web Driver issue in Threading. C#

Here's the code.
browser = new FirefoxDriver();
browser.Navigate().GoToUrl("https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry");
Thread.Sleep(5000);
browser.FindElement(By.Name("ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown")).SendKeys("asdf");
It works ok but if I run in thread it shows element not visible.... why it's throwing in a thread?
Element could be non-visible cuz page didnt reload at check moment or website using dynamic names, classes etc.
You can try something like this:
IWebDriver browser = new FirefoxDriver();
browser.Navigate().GoToUrl("https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry");
while ( true ) {
try {
browser.FindElement(By.Name("ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown")).SendKeys("asdf");
break;
}
catch { Thread.Sleep(1000);}
}
Going by the xpath you tried out, it seems the name attribute is dynamic. To locate the text box for Registration number You can try either of the following options :
CssSelector :
browser.FindElement(By.CssSelector("input[class=text text xlong v_registrationNumber v_required][id^=ph_pagebody_)]")).SendKeys("asdf");
XPath :
browser.FindElement(By.XPath("//input[#class='text text xlong v_registrationNumber v_required'][starts-with(#id, 'ph_pagebody_')]")).SendKeys("asdf");

c# with Selenium: element not visible

I'm trying to create an automated ui test with selenium in c#. Here is my code:
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("my_url");
driver.FindElementById("textBox").Clear();
driver.FindElementById("textBox").SendKeys("tire");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((ExpectedConditions.ElementIsVisible(By.Id("Moto"))));
driver.FindElementById("Moto").Click();
Before using a wait.until, I was getting the exception ElementNotVisibleException, but now I'm getting the exception WebDriverTimeoutException because the element with the id "Moto" is not visible.
Here is a screenshot of a part of the DOM:
So why the moto checkbox is not found or is not visible?
Try the below code (in Java) as it is working at my end for the same structure -
driver.findElement(By.xpath("//label[#for='Moto']")).click();
I don't know why it is causing problem to find <input> tag by id
You might need to scroll to the element to make it visible
IWebElement moto = driver.FindElement(By.Id("Moto"));
Actions actions = new Actions(driver);
actions.MoveToElement(moto).Perform();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Moto"))).Click();

In Selenium IDE, how to make a wait command in selenium test until a browser reloads?

I have used the Selenium IDE to generate some code and I am now i want to put a wait command until the browser reloads after a button click. For example:
public void TheGetTest()
{
driver.Navigate().GoToUrl(baseURL + "/");
driver.FindElement(By.XPath("(//a[contains(text(),'Get Quotes')])[2]")).Click();
}
After the Click command, I want to see what URL is loaded. Is this possible or do I have to have to use some element on the page to look for?
WebDriverWait wait5 = new WebDriverWait(driver, 100);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>(d => d.FindElement(By.Id("someDynamicElement")));
How would this code be used if the dynamic element is the page itself?
Its been some time since i used selenium.
But maybe you can use a javascript function to determine if the document is in ready state again.
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));

Categories

Resources