【问题标题】:Robot Framework - unable to locate React created elements机器人框架 - 无法找到 React 创建的元素
【发布时间】:2019-02-18 10:17:00
【问题描述】:

我正在尝试将文本输入到 React 创建的元素中。到目前为止,我已经尝试了以下方法:

Input Text     css:input#textfield-1198-inputEl    TEXT
Input Text     css:input.x-form-field x-form-required-field x-form-#textfield-1198-inputEl   TEXT
Input Text      xpath=//input[contains(@name,'textfield-1198-inputEl')]   TEXT
Input Text     xpath=//*[@id="textfield-1198-inputEl"]   TEXT

除了尝试绝对路径和相对路径之外,虽然它总是响应 - 不匹配任何元素。 等到元素可见超时。

任何帮助将不胜感激。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="x-window x-layer x-window-default x-closable x-window-closable x-window-default-closable x-border-box x-resizable x-window-resizable x-window-default-resizable" id="window-1196" style="width: 316px; height: 195px; right: auto; left: 630px; top: 315px; z-index: 19001;" tabindex="-1">
        <div class="x-window-header x-header x-header-horizontal x-header-draggable x-docked x-unselectable x-window-header-default x-horizontal x-window-header-horizontal x-window-header-default-horizontal x-top x-window-header-top x-window-header-default-top x-docked-top x-window-header-docked-top x-window-header-default-docked-top" id="window-1196_header" style="right: auto; left: 0px; top: 0px; width: 316px;">
            <div class="x-header-body x-window-header-body x-window-header-body-default x-window-header-body-horizontal x-window-header-body-default-horizontal x-window-header-body-top x-window-header-body-default-top x-window-header-body-docked-top x-window-header-body-default-docked-top x-box-layout-ct x-window-header-body-default-horizontal x-window-header-body-default-top x-window-header-body-default-docked-top" id="window-1196_header-body" style="width: 309px;">
                <div class="x-box-inner" id="window-1196_header-innerCt" role="presentation" style="width: 309px; height: 15px;">
                    <div class="x-box-target" id="window-1196_header-targetEl" style="width: 309px;">
                        <div class="x-component x-header-text-container x-window-header-text-container x-window-header-text-container-default x-box-item x-component-default" id="window-1196_header_hd" style="right: auto; left: 0px; top: 0px; margin: 0px; width: 288px;">
                            <span class="x-header-text x-window-header-text x-window-header-text-default" id="window-1196_header_hd-textEl">&nbsp;</span>
                        </div>
                        <div class="x-tool x-box-item x-tool-default x-tool-after-title" id="tool-1203" style="width: 15px; height: 15px; right: auto; left: 294px; top: 0px; margin: 0px;"><img class="x-tool-img x-tool-close" id="tool-1203-toolEl" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="x-window-body x-window-body-default x-closable x-window-body-closable x-window-body-default-closable x-window-body-default x-window-body-default-closable x-resizable x-window-body-resizable x-window-body-default-resizable" id="window-1196-body" style="overflow: auto; left: 0px; width: 316px; height: 165px; top: 30px;">
            <span id="window-1196-outerCt" style="display:table;"></span>
            <div class="" id="window-1196-innerCt" style="height: 100%; vertical-align: top; display: table-cell;">
                <span id="window-1196-outerCt" style="display:table;"></span>
                <div class="x-panel x-window-item x-panel-default" id="createCMDBPanel-1197" style="width: 316px; height: 165px;">
                    <span id="window-1196-outerCt" style="display:table;"></span>
                    <div class="x-panel-body x-panel-body-default x-panel-body-default x-docked-noborder-top x-docked-noborder-right x-docked-noborder-bottom x-docked-noborder-left" id="createCMDBPanel-1197-body" style="padding: 0px; left: 0px; top: 0px; width: 316px; height: 125px;">
                        <span id="window-1196-outerCt" style="display:table;"><span id="createCMDBPanel-1197-outerCt" style="display:table;"></span></span>
                        <div class="" id="createCMDBPanel-1197-innerCt" style="height: 100%; vertical-align: top; padding: 10px; display: table-cell;">
                            <span id="window-1196-outerCt" style="display:table;"></span>
                            <table cellpadding="0" class="x-field x-table-plain x-form-item x-form-type-text x-field-default x-anchor-form-item" id="textfield-1198" style="table-layout: auto;">
                                <tbody>
                                    <tr class="x-form-item-input-row" id="textfield-1198-inputRow" role="presentation">
                                        <td class="x-field-label-cell" id="textfield-1198-labelCell" role="presentation" style="" valign="top" width="105"><label class="x-form-item-label x-unselectable x-form-item-label-left" for="textfield-1198-inputEl" id="textfield-1198-labelEl" style="width:100px;margin-right:5px;">CMDB Name:</label></td>
                                        <td class="x-form-item-body" colspan="2" id="textfield-1198-bodyEl" role="presentation"><input aria-invalid="false" autocomplete="off" class="x-form-field x-form-required-field x-form-text" data-errorqtip="" id="textfield-1198-inputEl" name="textfield-1198-inputEl" type="text" value="new CMDB"></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

【问题讨论】:

  • 这是实际的错误信息吗?尽可能精确。

标签: selenium xpath robotframework


【解决方案1】:

一般来说,React 放置的属性值是“半动态的”(我的术语,不要引用我的话 :) - 它们是由框架按照它认为合适的方式生成的,并且可以在轻微的源代码更改时更改.这适用于class,在您的情况下适用于id 属性 - 正如您所见,其中的数字 1198 没有语义意义,它只是一个计数器。
因此,您最好不要在定位器中使用它们,因为它们不会是刚性的 - 值可以在构建之间轻松更改而不会发出警告。

我会用不同的策略来处理它;这是一个有效的 xpath 定位器,其结构分解如下:

//td[label[text()="CMDB Name:"]]/following-sibling::td/input[@type="text"]

它首先选择一个表格单元格,该单元格的子单元格为 span 和该文本 - 用户在 UI 中看到的输入标签。然后它选择下一个单元格(following-sibling 轴),最后 - 子元素的 input 元素 - 你的目标。

实际上,此定位器读取 - “返回文本'CMDB Name:'旁边的输入”

【讨论】:

  • 嗨,@Todor!因此,使用当前的 React 方法,我们通过分类、id 和 DOM 遍历所拥有的所有优势……消失了?我们回到解析文本?这真是个坏消息:(
  • 我不会说这很糟糕; ids 可以固定,而且开发人员总是可以设置一个自定义元素属性,该属性具有用于定位器的预定义值。类 - 它们是禁区(但一直如此,至少对于 == 比较而言)。
  • 实用提示:安装 Selenium IDE,并记录操作。打开项目文件后,可以: Input Text xpath=//span/div/table/tbody/tr/td[2]/input ...project options = "targets": [ ["id=textfield-1198 -inputEl", "id"], ["name=textfield-1198-inputEl", "name"], ["css=#textfield-1198-inputEl", "css"], ["xpath=//input[ @id='textfield-1198-inputEl']", "xpath:attributes"], ["xpath=//td[@id='textfield-1198-bodyEl']/input", "xpath:idRelative"], ["xpath=//span/div/table/tbody/tr/td[2]/input", "xpath:position"] ],
猜你喜欢
  • 2018-08-16
  • 2015-11-11
  • 2014-09-29
  • 2018-12-04
  • 2019-05-02
  • 2016-04-26
  • 2021-11-22
  • 2020-12-16
  • 2021-03-16
相关资源
最近更新 更多