Reputation: 13
I am testing a web page with dynamic ids. The page contains a remove icon followed by a browse icon at multiple places and I am not able to click these icons or I must say I am not able to write the correct XPath to locate any particular icon.
The following is a snippet of the HTML code.
<tr class="tableNestedAttribute">
<td class="autosuggest-svl-cell">
<input name="odf_obs_fin_dept_text">
<div id="div_d31505e760_location"></div>
</td>
<td class="actions" nowrap="" align="right">
<div class="ui-browse-lookup-images ppm_field formFieldDisNoWidthReadOnly ">
<img id="d31505e7601" title="Remove Department OBS" name="Remove">
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
</div>
</td>
</tr>
The above HTML code is at multiple places in the page except that the name in the first td is different at each place.
My requirement is here to click the browse which is in the same line as the input field with name odf_obs_fin_dept_text
.
Tried the following and various other combinations but with no luck.
driver.findElement(By.xpath("//*[@name='odf_obs_fin_dept_text']/following-sibling::td[1]/descendant::div/img[@name='Browse']")).click();
Upvotes: 0
Views: 617
Reputation: 4739
With the provided example the following XPath:
//input[@name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/div/img[@name='Browse']
will find the next element:
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
Or if you want to keep your XPath intact, only add parent::td
between //*[@name='odf_obs_fin_dept_text']
and following-sibling::td[1]
//*[@name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/descendant::div/img[@name='Browse']
Upvotes: 1