【发布时间】: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