续例 - 找不到watir元素

时间:2015-04-07 23:43:57

标签: ruby watir watir-webdriver

Cannot click html element with watir中,我试图点击一个元素,它给我下面的行:

enter image description here

我无法找到您在上图中看到的三个下拉列表。 我该怎么做 ?每次,下拉元素的ID都会改变。

例如,中间的一个HTML代码是:

<td>
<select id="filtersJob6_intrinsic_enumOperator" name="filtersJob6_intrinsic_enumOperator" onchange="if(top.document.getElementById('filtersJob6_intrinsic_enumOperator').value=='isNull'||top.document.getElementById('filtersJob6_intrinsic_enumOperator').value=='isNotNull'){top.document.getElementById('filtersJob6_intrinsic_operand1Container').style.display='none';}else{top.document.getElementById('filtersJob6_intrinsic_operand1Container').style.display='inline';}">
<option value="equals" selected="selected">equals</option>
<option value="notEqual">not equals</option>
<option value="isNull">is not set</option>
<option value="isNotNull">is set</option>
</select>
</td>

ID id="filtersJob6_intrinsic_enumOperator"可以包含任何数字4,7,6,5等。

如何让watir找到这个元素?

编辑 -

Here is the code for the first dropdown:

<select id="filtersJob3_intrinsic_name" name="filtersJob3_intrinsic_name" onchange="var shown=0;top.document.getElementById('filtersJob3_intrinsic_operator').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='none';top.document.getElementById('filtersJob3_intrinsic_dateOperator').style.display='none';top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='none';top.document.getElementById('filtersJob3_intrinsic_listOperator').style.display='none';top.document.getElementById('filtersJob3_intrinsic_uuidOperator').style.display='none';top.document.getElementById('filtersJob3_intrinsic_datehelp1').style.display='none';top.document.getElementById('filtersJob3_intrinsic_datehelp2').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1abortStatus').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1outcome').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1priority').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1status').style.display='none';top.document.getElementById('filtersJob3_intrinsic_operand1errorCode').style.display='none';if(top.document.getElementById('filtersJob3_intrinsic_name').value=='abortStatus'){shown=1;top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1abortStatus').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='outcome'){shown=1;top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1outcome').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='priority'){shown=1;top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1priority').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='status'){shown=1;top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1status').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='errorCode'){shown=1;top.document.getElementById('filtersJob3_intrinsic_enumOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1errorCode').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='jobId'){shown=1;top.document.getElementById('filtersJob3_intrinsic_uuidOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2Container').style.display='none';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='createTime'){shown=1;top.document.getElementById('filtersJob3_intrinsic_dateOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp2').style.display='inline';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='finish'){shown=1;top.document.getElementById('filtersJob3_intrinsic_dateOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp2').style.display='inline';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='modifyTime'){shown=1;top.document.getElementById('filtersJob3_intrinsic_dateOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp2').style.display='inline';}if(top.document.getElementById('filtersJob3_intrinsic_name').value=='start'){shown=1;top.document.getElementById('filtersJob3_intrinsic_dateOperator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_datehelp2').style.display='inline';}if(!shown){top.document.getElementById('filtersJob3_intrinsic_operator').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operator').onchange();top.document.getElementById('filtersJob3_intrinsic_operand1').style.display='inline';top.document.getElementById('filtersJob3_intrinsic_operand2').style.display='inline';}">

  <option value="abortStatus" selected="selected">Abort Status</option>
  <option value="abortedBy">Aborted By</option>
  <option value="createTime">Create Time</option>
  <option value="directoryName">Directory Name</option>
  <option value="elapsedTime">Elapsed Time</option>
  <option value="errorCode">Error Code</option>
  <option value="errorMessage">Error Message</option>
  <option value="finish">Finish Date &amp; Time</option>
  <option value="credentialName">Impersonation Credential</option>
  <option value="jobId">Job ID</option>
  <option value="jobName">Job Name</option>
  <option value="lastModifiedBy">Last Modified By</option>
  <option value="launchedByUser">Launched By User</option>
  <option value="licenseWaitTime">License Wait Time</option>
  <option value="liveProcedure">Live Procedure</option>
  <option value="liveSchedule">Live Schedule</option>
  <option value="modifyTime">Modify Time</option>
  <option value="outcome">Outcome</option>
  <option value="owner">Owner</option>
  <option value="priority">Priority</option>
  <option value="procedureName">Procedure Name</option>
  <option value="projectName">Project Name</option>
  <option value="resourceWaitTime">Resource Wait Time</option>
  <option value="runAsUser">Run As User</option>
  <option value="scheduleName">Schedule Name</option>
  <option value="start">Start Date &amp; Time</option>
  <option value="stateName">State Name</option>
  <option value="status">Status</option>
  <option value="totalWaitTime">Total Wait Time</option>
  <option value="workspaceWaitTime">Workspace Wait Time</option>
</select>

1 个答案:

答案 0 :(得分:2)

您可以在定位器中使用正则表达式:

browser.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select value_to_select

在此示例中,正则表达式由开始和结束正斜杠表示,原始字符串中的数字替换为\d+,表示“一个或多个数字”。

修改

给出这个最小的HTML片段:

<select id="filtersJob6_intrinsic_enumOperator">
  <option value="isNull">is not set</option>
  <option value="isNotNull">is set</option>
</select>

使用select根据文字选择选项:

b.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select 'is set'

使用select_value根据value属性选择一个选项:

b.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select_value 'isNotNull'