我在尝试单击单选按钮时遇到了麻烦。
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
但它不起作用...我也尝试使用一段文字作为引用,但效果不佳。
你们对如何执行它有什么建议吗? 非常感谢!
最佳答案
But it's not working
是的。当页面定义这样的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 脚本中使用该值。
关于javascript - 使用 Watir 单击具有变量值的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797890/