> ## Documentation Index
> Fetch the complete documentation index at: https://rpa.bazhuayu.com/helpcenter/llms.txt
> Use this file to discover all available pages before exploring further.

# 获取相关元素

## 指令说明

**描述：** 主要用于少数无法直接捕获到的网页元素，可通过捕获其父元素、子元素、相邻元素来关联获取。

## 参数说明

<Tabs>
  <Tab title="常规">
    <img src="https://mintcdn.com/bazhuayu-rpa-docs/_udziDO1jBiOpIxe/helpcenter/commands/web-automation/images/web-relative-01.png?fit=max&auto=format&n=_udziDO1jBiOpIxe&q=85&s=d98be1caaa89ed006412d07b863c3586" alt="获取相关元素指令参数面板" width="660" height="479" data-path="helpcenter/commands/web-automation/images/web-relative-01.png" />

    | 参数       | 说明                                                                                                                                                                                                                                                              |
    | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | **网页对象** | 选择要操作的目标元素所在的网页对象                                                                                                                                                                                                                                               |
    | **选择元素** | 选择要操作的目标元素：从「元素库」中选择一个已定义的元素或通过「捕获新元素」来捕获新的网页元素                                                                                                                                                                                                                 |
    | **关联方式** | 下拉选择如何定位指定元素的关联元素：<br />父元素：关联网页中上一级元素；<br />前一个相邻元素：关联相联的前一个元素；<br />后一个相邻元素：关联相邻的后一个元素；<br />第一个匹配的子孙元素：关联网页中的下一级元素。可以设置相对xpth子孙元素相对于所选元素的相对xpath即在所选元素内去用指定Xpath去定位某个子孙元素；<br />所有子元素：关联所有子元素；<br />指定位置的子元素：关联指定位置的子元素。可以设置子元素位置，即子元素在父元素(当前选择的元素) 的里的下标位置； |
  </Tab>

  <Tab title="高级">
    | 参数            | 说明            |
    | ------------- | ------------- |
    | **等待元素存在（s）** | 等待目标元素存在的超时时间 |
  </Tab>
</Tabs>

## 使用示例

<img src="https://mintcdn.com/bazhuayu-rpa-docs/_udziDO1jBiOpIxe/helpcenter/commands/web-automation/images/web-relative-02.png?fit=max&auto=format&n=_udziDO1jBiOpIxe&q=85&s=bd9d7f1d3f030c7eefe629da1a39e0c9" alt="获取相关元素使用示例 - 流程搭建" width="919" height="293" data-path="helpcenter/commands/web-automation/images/web-relative-02.png" />

**该流程执行逻辑：**

打开网页 ，获取网页对象

使用【循环相似元素(web)】指令获取百度首页的热搜榜单

在循环体中对每个「当前网页元素」使用【获取相关元素】指令，关联方式「第一个匹配的子孙元素」填入「相对XPath」来获取到百度热搜榜单的文本元素

使用【打印日志】将网页关联元素.Text打印出来

### 效果展示

成功通过参考元素获取关联的目标元素（父/子/相邻元素）。

<img src="https://mintcdn.com/bazhuayu-rpa-docs/_udziDO1jBiOpIxe/helpcenter/commands/web-automation/images/web-relative-03.png?fit=max&auto=format&n=_udziDO1jBiOpIxe&q=85&s=2351ae6499348f72bdf8096435aa8014" alt="获取相关元素使用示例 - 效果展示" width="650" height="524" data-path="helpcenter/commands/web-automation/images/web-relative-03.png" />

<img src="https://mintcdn.com/bazhuayu-rpa-docs/_udziDO1jBiOpIxe/helpcenter/commands/web-automation/images/web-relative-04.png?fit=max&auto=format&n=_udziDO1jBiOpIxe&q=85&s=a73b60052629030d31a54ed469eaaacc" alt="获取相关元素使用示例 - 效果展示 2" width="1398" height="850" data-path="helpcenter/commands/web-automation/images/web-relative-04.png" />

<img src="https://mintcdn.com/bazhuayu-rpa-docs/_udziDO1jBiOpIxe/helpcenter/commands/web-automation/images/web-relative-05.png?fit=max&auto=format&n=_udziDO1jBiOpIxe&q=85&s=67c0956792984469b0182283eab3488e" alt="获取相关元素使用示例 - 效果展示 3" width="1351" height="417" data-path="helpcenter/commands/web-automation/images/web-relative-05.png" />

<Info>
  使用指令过程中遇到问题？前往 [八爪鱼RPA 开发者社区问答板块](https://rpa.bazhuayu.com/community/questions) 提问，获取官方与社区帮助。
</Info>
