0

我正在使用 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 代码,但仍然单击“重命名”传递并单击“取消”失败。为什么会这样?

4

2 回答 2

0

您可以尝试以下方法:

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
于 2013-07-08T20:05:46.573 回答
0

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

driver.find_element(:xpath, "//span[text()='Cancel']").click
于 2013-07-09T08:51:41.137 回答