使用 Watir 单击具有变量值的单选按钮

Using Watir to click on Radio Buttons with variable values

本文关键字:变量值 单选按钮 Watir 单击 使用      更新时间:2023-09-26

我在尝试单击单选按钮时遇到了麻烦。

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/