【问题标题】:Unable to click on 'Cancel' button无法单击“取消”按钮
【发布时间】:2013-07-09 16:51:41
【问题描述】:

我正在使用 Selenium WebDriver 和 Ruby 编写自动化脚本。在这种情况下,我必须点击“取消”按钮,下面是它的 html 代码:

<div class="ui-dialog-buttonset">
 <button class="otherButtonClass" type="button" role="button" aria-disabled="false">
  <span class="ui-button-text">Rename</span>
 </button>
 <button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
  <span class="ui-button-text">Cancel</span>
 </button>
</div>

为了点击“取消”按钮,我写了以下内容:

driver.find_element(:xpath, "//button[@class='cancelButtonClass']").click

此处不会发生单击操作。我试过睡觉,wait.until { element.displayed? } 仍然没有解决问题。抛出的错误是“元素不可见,因此可能无法与之交互”

但是,如果我在“重命名”按钮上执行点击操作,它会起作用:

driver.find_element(:xpath, "//button[@class='otherButtonClass']").click

请帮助我理解为什么会这样。我很困惑,“重命名”和“取消”具有相似的 html 代码,但仍然单击“重命名”通过并单击“取消”失败。为什么会这样?

【问题讨论】:

    标签: ruby selenium-webdriver


    【解决方案1】:

    你可以试试下面的:

    script = <<-end
    element = arguments[0];
    element.setAttribute('aria-disabled','true');
    return element;
    end
    
    # select the 'Cancel' button element
    elem = driver.find_element(:css,'div.ui-dialog-buttonset>button')[1]
    # setting the 'aria-disabled' to true
    elem = driver.execute_script(script,elem)
    #after enabling the css attribute 'aria-disabled' click on the
    #cancel button
    elem.click
    

    【讨论】:

      【解决方案2】:

      如果按钮 CSS 对于任何悬停动作都是动态的,则使用 CSS 选择将不是一个完美的解决方案。此外,选择给定元素的简单方法是使用以下 xpath。

      driver.find_element(:xpath, "//span[text()='Cancel']").click
      

      【讨论】:

      • 是的,我同意,我建议只有在按钮的 CSS 是动态的情况下才使用该解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多