【问题标题】:OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError at WebElement Submit clickOpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError 在 WebElement 提交单击
【发布时间】:2019-04-28 18:13:14
【问题描述】:

我正在尝试使用 C#、Selenium 和 Chrome Webdriver 为某些网页自动化一个场景,其中单击页面上的提交按钮将提交 sendkeys 值。
但是,当单击提交时,它会在 Visual Studio 测试资源管理器窗口 (Nunit) 中引发 UnpackAndThrowOnError 错误。

    <button type="submit" class="btn btn-primary mr-4" name="postType" value="Submit">Submit<span class="glyphicon glyphicon-floppy-save"></span></button>

详细错误说明:

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

我已经尝试了以下代码选项,但它们都在 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();

我也尝试过将submitBtnReview.Submit() 与上面的IwebElement 一起使用,但是它会使页面崩溃。

我正在使用最新版本的 Selenium.WebDriver (3.141.0) 和 Selenium.Chrome.WebDriver (2.43.0),并在 windows 10 机器上使用 chrome 版本 70.0.3538.102 (Official Build) (64-bit) .

知道我在这里可能做错了什么吗?


[2018 年 5 月 12 日] - 感谢您提供所有宝贵的反馈 - 我现在也尝试了以下方法,但它们在 submitBtnReview.Click() 失败并出现新错误:

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

设置一些断点,显示submitBtnReviewLocationOnScreen 坐标与新错误,这可能与此有关。

    LocationOnScreen = '((OpenQA.Selenium.Remote.RemoteCoordinates)((OpenQA.Selenium.Remote.RemoteWebElement)submitBtnReview ).Coordinates).LocationOnScreen' threw an exception of type 'System.NotImplementedException'

从调试提交按钮详细信息:

错误图片:

希望这些附加信息有助于找到原因 - 提前致谢!!

【问题讨论】:

  • "...崩溃页面..." - 这些信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
  • @JohnB,感谢您的评论。我已经更新了描述以正确反映问题。我在 Visual Studio 测试资源管理器窗口 (Nunit) 中遇到错误。在成功的网页提交点击我期待移动看到网页上的提交没有。

标签: c# selenium selenium-webdriver xpath css-selectors


【解决方案1】:

目前还没有确切的答案,只是一些观察。 3 个给定的定位器看起来不正确,虽然 1 个看起来没问题。

1) By.XPath("//button[contains(.,'Submit')]")); ---> 看起来正确 --- explained here

建议:如果文本值被任何给定元素 class 值修改并且按钮在屏幕上显示为“提交”(全部大写)或提交(全部小写),您应该在 xpath 中也可以使用它作为 "//button[contains(.,'SUBMIT')]" 或 "//button[contains(.,'submit')]" 。

2) By.CssSelector("input[value='Submit']")); ---> By.CssSelector("button[value='Submit']")); --- 你的标签是&lt;button&gt; 不是&lt;input&gt; p>

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']")); --- 你的标签是&lt;button&gt; 不是&lt;input&gt; explained here

在点击之前,确保元素是可点击的

public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
    return driver =>
    {
        var element = driver.FindElement(locator);
        return (element != null && element.Displayed && element.Enabled) ? element : null;
    };
}

可用于:

var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable(By.Id("id"))

original source

[已编辑问题后添加] 该错误表明您有一个元素,并且 selenium 无法在该点上单击它。 the problem is described and solved here

在我看来,最稳定但最简单的方法是尝试使用 javascript 点击(如果屏幕上有元素,它只需 必须点击,并根据您确实拥有的示例):

IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);

另一种情况是确保您的元素位于浏览器的可见区域(您可以在调试期间查看/单击它无需任何手动滚动)。如果它不可见或不完全可见 - 在点击之前滚动到它

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView()", Webelement); 

双重检查建议:确保只有一个元素具有这样的定位器。通常的情况是当你认为你找到了“正确的”元素,但 selenium 找到了第一个匹配项,它不是你想要的。

另一个建议是你的元素可能会在你找到它之后改变它的位置。所以你找到了它,保存到一个变量中,然后由于某种原因页面布局发生了变化(例如,异步加载完成)。您可以仔细注意页面的加载方式。布局有变化吗?如果是 - 您可以创建一个自定义等待函数,该函数将检查元素坐标是否在 1-2 秒内发生变化。

【讨论】:

  • 我已经尝试了上述所有选项,正如您在@Vladmir 中解释的那样,但在 submitBtnReview.Click() 处仍然出现相同的错误;
  • 不幸的是,错误日志要么未满,要么只是没有足够的详细信息,但目前该元素可能不可点击。更新了我的答案
  • 我已经用一些额外的发现更新了这个问题,我希望你能在这里再次帮助我。
  • @ManishSharma 用一些建议更新了我的答案
  • 我非常感谢你——你是救生员。提交按钮不在浏览器的可见区域,添加 Javascript 滚动解决了我的问题。
【解决方案2】:

该元素看起来是一个动态元素,因此您需要诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一解决方案:

  • 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();
    
  • 注意:当您使用 ChromeDriver v2.43 时,请确保您使用的是 Chrome v69-71

【讨论】:

  • 我已经用一些额外的发现更新了这个问题,我希望你能再次在这里帮助我。
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多