Search code examples
c#selenium-webdriverxpathcss-selectors

OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError at WebElement Submit click


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>

enter image description here

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:

enter image description here

Error image: enter image description here

Hope this additional information helps in finding the cause.


Solution

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