0

我正在测试一个带有动态 id 的网页。该页面在多个位置包含一个删除图标,后跟一个浏览图标,我无法单击这些图标,或者我必须说我无法编写正确的 XPath 来定位任何特定图标。

以下是一段 HTML 代码。

<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>

上面的 HTML 代码在页面中的多个位置,除了第一个 td 中的名称在每个位置都不同。

我的要求是在这里单击与带有 name 的输入字段在同一行的浏览odf_obs_fin_dept_text

尝试了以下和其他各种组合,但没有运气。

driver.findElement(By.xpath("//*[@name='odf_obs_fin_dept_text']/following-sibling::td[1]/descendant::div/img[@name='Browse']")).click();
4

1 回答 1

1

使用提供的示例,使用以下 XPath:

//input[@name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/div/img[@name='Browse']

将找到下一个元素:

<img id="d31505e7600" title="Browse Department OBS" name="Browse">

或者如果你想保持你的 XPath 完整,只在和parent::td之间添加//*[@name='odf_obs_fin_dept_text']following-sibling::td[1]

//*[@name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/descendant::div/img[@name='Browse']
于 2013-11-08T11:38:19.473 回答