【问题标题】:Unable to click element XPath无法单击元素 XPath
【发布时间】:2019-08-02 20:44:24
【问题描述】:

这是我想要达到的元素:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
  <div class="ui-dialog-buttonset">
    <button style="background-color: rgb(218, 218, 218);" aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button">
        <span style="background-color: transparent;" class="ui-button-text">OK</span>
    </button>
  </div>
</div>

这是我正在使用的代码:

driver.FindElement(By.XPath("xpath=(//span[contains(@class,'ui-button-text')][contains(text(),'OK')]))")).Click();

我通过 xpath 使用了 Selenium IDE 的查找元素功能,它可以找到元素。

【问题讨论】:

    标签: c# selenium xpath


    【解决方案1】:

    表达式中不需要xpath= 部分:

    driver.FindElement(By.XPath("//span[contains(@class,'ui-button-text')][contains(text(),'OK')])")).Click();
    

    另外,我认为您可以停止使用 contains() 并检查完整的 classtext() 值:

    driver.FindElement(By.XPath("//span[@class = 'ui-button-text' and . = 'OK'])")).Click();
    

    这里的. 指的是元素的文本。

    【讨论】:

    • 虽然,必要的警告,如果类可能包含更多的类,你将不得不使用 contains()。
    【解决方案2】:

    其他解决方案是使用这个xpath:

    driver.FindElement(By.XPath("///div[@class='ui-dialog-buttonset']/button/span")).Click();
    

    【讨论】:

      【解决方案3】:

      如果您想要准确的解决方案: 你可以用这个:

      driver.FindElement(By.Xpath("//div[@class='ui-dialog-buttonset']/descendant::span[@class='ui-button-text' and contains(text(),'OK')]/parent::button")
      

      【讨论】:

      • 这个帖子是 2 年前的,我想他们现在已经弄清楚了,或者重新编写了应用程序。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多