【问题标题】:Not able to select through CSS Selector from webdriver无法通过 webdriver 的 CSS 选择器进行选择
【发布时间】:2015-08-05 14:42:27
【问题描述】:

我无法找到包含“确认”和“取消”的元素。 另外,不想使用 Xpath。谁能建议我获得该元素的最佳方法。 以下是 HTML -

   <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
   <div class="ui-dialog-buttonset">
   <button class="btn-confirm-dialog ui-button ui-widget ui-state-default ui-corner-all ui-  button-text-only" type="button" role="button" aria-disabled="false">
   <span class="ui-button-text">Confirm</span>
   </button>
   <button class="btn-confirm-dialog ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
   <span class="ui-button-text">Cancel</span>
   </button>
   </div>

【问题讨论】:

    标签: css selenium-webdriver css-selectors


    【解决方案1】:

    您不能通过 CSS 文本来选择元素。
    试试这些 CSS 选择器:
    确认:button span,虽然取消按钮也匹配这个,但 WebDriver.findElement 方法在找到第一个匹配元素时停止。
    取消:button + button span

    【讨论】:

      【解决方案2】:

      您可以更改您的 html 设计以在按钮标记内添加链接 (&lt;a href="xxx"&gt;...&lt;/a&gt;)。比您可以使用硒定位器“通过链接文本”获取 webelement http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

      【讨论】:

        【解决方案3】:

        确认按钮 -- css=button.btn-confirm-dialog > span:contains("Confirm") 取消按钮 -- css=button.btn-confirm-dialog > span:contains("Cancel")

        以上是确认和取消按钮的 CSS 选择器。

        【讨论】:

          【解决方案4】:

          我通过cssSelector 的“nth-child”属性得到了这个。

          Confirm =>  div.ui-dialog-buttonset> button:nth-child(1) >Span
          
          Cancel => div.ui-dialog-buttonset> button:nth-child(2) >Span
          

          感谢您的宝贵回答。

          【讨论】:

            猜你喜欢
            • 2013-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-27
            • 2015-10-20
            相关资源
            最近更新 更多