gpt4 book ai didi

ruby - 如何使用带有 Selenium Watir-webdriver 的 Ruby 执行 Ctrl + 多次鼠标单击操作?

转载 作者:太空宇宙 更新时间:2023-11-03 16:00:21 25 4
gpt4 key购买 nike

我遇到了一个困难,我必须使用带有 Ruby 的 watir-webdriver 执行 Ctrl+鼠标单击操作

在我的网络应用程序中,我必须使用 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

enter image description here

最佳答案

我假设该控件的行为类似于可选择的 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 的点击方法。修饰符可用于告诉 Watir 在单击元素时按住某些键。例如,以下将在单击每个 li 时按下 control:

browser.lis.each do |li|
li.click(:control)
end

请注意,这在技术上与选项 1 中的用户行为不同。在选项 1 中,在单击所有列表时按住控制按钮。相反,选项 2 将按下控制按钮,单击元素,释放控制按钮,然后重复下一个元素。根据应用程序的实现,它可能会或可能不会关心差异。

关于ruby - 如何使用带有 Selenium Watir-webdriver 的 Ruby 执行 Ctrl + 多次鼠标单击操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26886084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com