使用 Watir 单击具有变量值的单选按钮
Using Watir to click on Radio Buttons with variable values
我在尝试单击单选按钮时遇到了麻烦。
HTML 代码是:
function radioButtonFormatter(el, oRecord, oColumn, oData) {
var checkFalse='checkFalse';
var check='check' ;
el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";
}
我想使用类似的东西:
browser.radio(:value => "oData").click
但它不起作用...我也尝试使用一段文字作为参考,但它没有很好地工作。
你们对如何执行它有什么建议吗?多谢!
但它不起作用
是的。 当页面定义一个 js 函数时,如下所示:
function radioButtonFormatter(el, oRecord, oColumn, oData) {
var checkFalse='checkFalse';
var check='check' ;
el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";
}
。它不会导致函数执行。 如果您执行了此 ruby 程序:
def greet
puts 'hello'
end
。你会想知道为什么没有输出吗? 同样,在执行 js 函数之前,单选按钮不存在。
接下来,这个:
browser.radio(:value => "oData").click
查找具有以下属性的单选按钮:
<radio value="oData" ...>
你想要的是:
browser.radio(:value => oData).click
但是,这意味着您必须创建一个名为 oData 的变量,并为其分配一个值:
oData = "something here"
browser.radio(:value => oData).click
在 ruby 程序中编写变量名 oData 并不会神奇地使其与 javascript 程序中名为 oData 的变量具有相同的值。
以下是您可以执行的一些操作:
3.htm:
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<script type="text/javascript">
function disableButtons(x, y) {
document.getElementById("disable-info").innerHTML = x + " " + y;
}
function radioButtonFormatter(el, oData) {
var checkFalse='checkFalse';
var check='check' ;
el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";
}
</script>
</head>
<body>
<div>Hello world</div>
<div id="div1"></div>
<div id="disable-info"</div>
</body>
</html>
.
require 'watir-webdriver'
b = Watir::Browser.new :firefox
b.goto "http://localhost:8080/3.htm"
b.execute_script(
"radioButtonFormatter(
document.getElementById('div1'), 42
)"
)
b.radio(:value => "42").click
或者,如果 radioButtonFormatter() 函数由某个事件触发,您可以使用 watir-webdriver 触发该事件:
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<script type="text/javascript">
function disableButtons(x, y) {
document.getElementById("disable-info").innerHTML = x + " " + y;
}
function radioButtonFormatter(el, oData) {
var checkFalse='checkFalse';
var check='check' ;
el.innerHTML="<input type='radio' name='table-radiobutton' value='" + oData + "' onclick='disableButtons(this.checked, this.type)' />";
}
window.onload = function() {
document.getElementById('greeting').onclick = function() {
radioButtonFormatter(document.getElementById('div1'), 42);
}
}
</script>
</head>
<body>
<div id="greeting">Hello world</div>
<div id="div1"></div>
<div id="disable-info"</div>
</body>
</html>
.
require 'watir-webdriver'
b = Watir::Browser.new :firefox
b.goto "http://localhost:8080/3.htm"
b.div(id: "greeting").when_present.click #This causes radioButtonFormatter() to execute
b.radio(value: "42").when_present.click
但这并不能让您控制 oData 的值。 您必须查看 js 以确定设置的 oData 值,然后在 ruby 脚本中使用该值。
试试这个:
el.innerHTML="<input id='rad' type='radio' name='table-radiobutton' value='val' />radio";
var rad = document.getElementById("rad");
rad.onclick=function(){
alert('clicked');
};
演示:http://jsfiddle.net/TZTGT/
相关文章:
- 多个单选按钮组相互干扰
- 是否可以禁用jquery中的单个单选按钮
- Javascript:无法获取变量中的单选按钮值
- 通过ajax到php文件获取单选按钮值
- 互斥单选按钮和相应的输入字段
- 用于选择MVC3中单选按钮值的JavaScript
- 如何获取单选按钮的多个实例的选定单选按钮值
- react testUtils模拟点击单选按钮而不触发onchange
- 当选择组中的单选按钮时,jQuery addClass
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 客户端单选按钮列表验证
- Javascript:图像与单选按钮的值不匹配
- 如何在Javascript中将单选按钮值保存到cookie中
- 在 ASPX 中从 JS 中删除单选按钮选定值后.cs所选值将保持不变
- 使用 Watir 单击具有变量值的单选按钮
- 当我选择不同的值时,如何使单选按钮变得清晰
- 变体下拉菜单变成单选按钮
- 如何使用单选按钮更改变量值
- 如果我单击单选按钮,如何使不可见的文本框变得可见?
- 使html的某些部分变灰,直到选中单选按钮