continued example - cannot find watir element
Asked Answered
B

1

0

In Cannot click html element with watir, i was trying to click an element which gives me the row below:

enter image description here

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 &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>
Buckwheat answered 7/4, 2015 at 23:43 Comment(0)
F
2

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'
Foregoing answered 8/4, 2015 at 0:20 Comment(7)
browser.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).select 'jobName' Error - `no_value_found': "jobName" not found in select list (Watir::Exception::NoValueFoundException). Adding the javascript and html code now.Buckwheat
Answer updated. You want to use select_value if you are trying to select based on the value attribute.Foregoing
Thanks. Now, I get the error no_value_found': "jobName" not found in select list (Watir::Exception::NoValueFoundException).Buckwheat
You're supplied 2 HTML snippets. The <select> tags have different id attributes. Make sure you are accessing the correct element.Foregoing
Yes, each time, the number in the id will be different as mentioned. o = browser.select_list(:id => /filtersJob\d+_intrinsic_enumOperator/).include? 'jobName'; puts o Output - falseBuckwheat
Select::include? will return true "if the select list has one or more options where text or label matches the given value". So, that won't work for value attributes. And--based on your HTML--<select id="filtersJob\d+_intrinsic_enumOperator"> doesn't contain <option value="jobName">Job Name</option>. That option is in <select id="filtersJobd\+_intrinsic_name">.Foregoing
my bad. browser.select_list(:id => /filtersJob\d+_intrinsic_name/).select 'Job Name' Does nothing. How do I find that element ?Buckwheat

© 2022 - 2024 — McMap. All rights reserved.