问题
In Cannot click html element with watir, i was trying to click an element which gives me the row below:
I am not able to find the three dropdown lists which you see in the image above. How do I do it ? Each time, the id of the dropdown elements changes.
For example, the middle one html code is:
<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>
The id id="filtersJob6_intrinsic_enumOperator"
can have any number 4,7,6,5 etc.
How do I make the watir find this element ?
EDIT -
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 & 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 & 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:
You can use a regular expression in your locator:
browser.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select value_to_select
In this example, the regular expression is denoted by opening and closing forward slashes, and the digit within the original string is replaced with \d+
, which means "one or more digits".
EDIT:
Given this minimal HTML snippet:
<select id="filtersJob6_intrinsic_enumOperator">
<option value="isNull">is not set</option>
<option value="isNotNull">is set</option>
</select>
Use select
to choose on option based on text:
b.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select 'is set'
Use select_value
to choose an option based on the value
attribute:
b.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select_value 'isNotNull'
来源:https://stackoverflow.com/questions/29503369/continued-example-cannot-find-watir-element