我遇到了一个困难,我必须使用 Ruby 的 watir-webdriver 执行 Ctrl+鼠标单击操作
在我的 Web 应用程序中,我必须使用 Ctrl 键加鼠标单击来选择多个选项(随机选项不是所有选项)。我可以看到多种使用 C# 或 Java 的解决方案。但我找不到任何使用 Ruby 和 Watir-webdriver 的解决方案。谁能帮我?
我尝试使用下面的代码 regionsArray=['航空公司'、'生物技术'、'金融集团'、'食品零售'、'餐馆'、'储蓄银行和烟草']
oPage.action.key_down(:control)
puts "hello2"
regionsArray.each { |x|
indXpath="//div[@id=('options-tree-region')]//div[text()='#{x}']"
indText = UtilsCommon.GetElementWithXpath(oPage, indXpath, 10, true)
if indText!= false
indText.click
end
最佳答案
我假设该控件的行为类似于 jQuery UI 可选择的并且将使用它们的 demo举个例子。
演示页面是:
<html lang="en">
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css">
<style>
#feedback { font-size: 1.4em; }
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>
<script>
$(function() {
$( "#selectable" ).selectable();
});
</script>
</head>
<body>
<ol class="ui-selectable" id="selectable">
<li class="ui-widget-content ui-selectee">Item 1</li>
<li class="ui-widget-content ui-selectee">Item 2</li>
<li class="ui-widget-content ui-selectee">Item 3</li>
<li class="ui-widget-content ui-selectee">Item 4</li>
<li class="ui-widget-content ui-selectee">Item 5</li>
<li class="ui-widget-content ui-selectee">Item 6</li>
<li class="ui-widget-content ui-selectee">Item 7</li>
</ol>
</body>
</html>
选项 1 - 使用 ActionBuilder
正如您所注意到的,您可以调用 Selenium-WebDriver ActionBuilder 来按下控件,然后单击元素。我猜测您的代码不起作用是因为从未为该操作调用 perform
方法。对于演示页面,按住控件并单击每个 li 将是:
# Press control (note the call to 'perform' the action)
browser.driver.action.key_down(:control).perform
# Click the elements
browser.lis.each(&:click)
为了控制被按下然后在最后释放,你也可以这样做:
action = browser.driver.action
action.key_down(:control)
browser.lis.each { |li| action.click(li.wd) }
action.key_up(:control)
action.perform
选项 2 - 使用点击修饰符
另一种解决方案是使用 Watir 的带有修饰符的 click 方法。这些修饰符可用于告诉 Watir 在单击某个元素时按住某些键。例如,以下内容将在单击每个 li 时按下 Control:
browser.lis.each do |li|
li.click(:control)
end
请注意,从技术上讲,这与选项 1 中的用户行为不同。在选项 1 中,在单击所有列表时按住控制按钮。相反,选项 2 将按下控制按钮,单击该元素,释放控制按钮,然后对下一个元素重复此操作。根据应用程序的实现,它可能关心也可能不关心差异。
关于ruby - 如何使用 Ruby 和 Selenium Watir-webdriver 执行 Ctrl+多次鼠标单击操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26886084/