web驱动程序c#xpth\css元素标识
我有以下html代码:web驱动程序c#xpth\css元素标识,c#,selenium,selenium-webdriver,css-selectors,C#,Selenium,Selenium Webdriver,Css Selectors,我有以下html代码: <br> input name="commit" value="Apply" style="" type="submit" <br> 我收到以下错误消息: 没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:”//input[@name='commit'][4]} 从注释中添加html: <form class="new_reduction" id="new_reduction" action="/admin/reduc
<br>
input name="commit" value="Apply" style="" type="submit"
<br>
我收到以下错误消息:
没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:”//input[@name='commit'][4]}
从注释中添加html
:
<form class="new_reduction" id="new_reduction" action="/admin/reductions" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="authenticity_token" value="mZHoKI82s1fk7w2FCsqbNiGM9kJyE45dzwV3w2cuFzniZe6lDDum9LDNxAsIokpxH90b/cFxqCil5EMvuvBwA==">
Coupon Code:
<input value="3167050" type="hidden" name="reduction[order_id]" id="reduction_order_id">
<input value="81069" type="hidden" name="reduction[web_user_id]" id="reduction_web_user_id">
<input type="text" name="code" id="code">
<input type="submit" name="commit" value="Apply">
</form>
库伊pon代码:
尝试不使用索引
driver.FindElement(By.XPath("//input[@name='commit']")).Click();
// or
driver.FindElement(By.Name("commit")).Click();
错误是什么?附加信息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“//input[@name='commit'][4]”是
iframe
中的元素吗?html标记应固定在该元素上。当您必须开始在xpath上使用[4]时,您的自动化变得非常脆弱。该输入应具有id=“fieldName”,且不应围绕元素。您可以做到这一点,但这方面的一项开发更改可能会扼杀您的自动化。如果我被迫这样写的话,我可能会看看前面的兄弟姐妹或后面的兄弟姐妹,这样就不会那么脆弱了。@DanielShmayovich看一下@Narendra的评论。有些选择器看起来是正确的,除非你给了我们错误的东西。因此,您可能处于
iframe
,frame
,或者您的驱动程序只是在错误的窗口中。发布完整的HTMLCOON代码:
driver.FindElement(By.XPath("//input[@name='commit']")).Click();
// or
driver.FindElement(By.Name("commit")).Click();