【问题标题】:WebElement not identified in selenium c#在 selenium c# 中未识别 WebElement
【发布时间】:2017-07-05 15:55:06
【问题描述】:

这是我正在使用的 HTML

        <div class="modal-footer">
<button class="btn btn-primary" type="button" value="Show Alert" ng-click="questions.getIrMessage()" 
data-dismiss="modal">Confirm</button>
<button class="btn btn-primary" type="button" ng-click="questions.showIrGrid()" data-dismiss="modal">Edit</button>
</div>

我正在尝试使用以下代码行查找“编辑”按钮

 [FindsBy(How = How.XPath, Using = "//button[contains(text(),'Edit')]")]
 public IWebElement btnEdit;

当我尝试使用此属性时,我得到了 No Element found 异常。

当我在代码中展开 btnEdit 属性时,我可以看到 'Displayed' 属性为 false 并且 text 属性为空 Property image

请查找附件。

但是当我尝试找到“确认”按钮时没有问题,我能够找到 webelement。

没有得到按钮webelemnt是什么原因。提前谢谢

【问题讨论】:

  • 确认按钮的显示属性是否相同?
  • 页面在浏览器中加载后,在开发控制台中运行$x("//button[contains(text(),'Edit')]").length。它返回 > 0 吗?查看加载页面的 HTML,你能看到元素以及为什么它可能被隐藏吗?您可能需要发布指向该页面的链接或更相关的 HTML。

标签: javascript c# html selenium


【解决方案1】:

谢谢大家的回复。有两个元素同名,这就是混淆的原因。实际上我想要第二个。下面是我用来点击我想要的按钮的代码 SeleniumWrapper.driver.FindElements(By.XPath("//button[contains(text(),'Edit')]"))[1].Click();

【讨论】:

    【解决方案2】:

    Selenium 模拟用户操作。您的元素对用户不可见(display=none),因此 Selenium 也无法读取它。

    您可以使用以下方法找到隐藏的元素:

    IList<IWebElement> hidden = driver.FindElements(By.CssSelector("button[style=display:none]"));
    

    您应该在此列表中找到您的元素。

    【讨论】:

    • 您在哪里看到按钮隐藏在提供的 HTML 中?即使是这样,你的代码也不会工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多