小编Lay*_*yup的帖子

对于存在的元素,什么可能导致“无法找到包含以下文本的标签”?

当元素确实存在时,什么可能导致getByLabelText测试库到\xc2\xa0报告找不到该元素?我得到的确切错误是

\n
\n

TestingLibraryElementError:无法找到带有以下文本的标签:删除项目:项目 C

\n
\n

它是在我运行测试后出现的,报告线路失败

\n
const removeItemButton = screen.getByLabelText(\'Remove Item: Item C\');\n
Run Code Online (Sandbox Code Playgroud)\n

测试输出包含\xc2\xa0included 的一大块页面源

\n
<span\n    class="my-company-span"\n    data-testid="custom-react-tag"\n>\n    Item: Item C\n    <button\n        aria-label="Remove Item: Item C"\n        class="myco-chp__button"\n        type="button"\n    >\n        ...\n
Run Code Online (Sandbox Code Playgroud)\n

为了进一步证明该元素确实存在,我最终使用不同的函数 ( getAllByRole) 来获取它,但我认为getByLabelText会更干净。

\n

该文档表示aria-label\xc2\xa0attributes 是getByLabelText. aria-label为了防止其中出现异常字符,我将错误输出中的名称直接复制粘贴到getByLabelText参数中,但没有任何变化。我getByLabelText()在同一文件中的其他元素上成功使用了该函数,因此这不是一个糟糕的导入。

\n

我在网上看到了很多类似但不一样的错误“无法找到带有文本的元素”的问题,但我发现针对我的情况的唯一问题是使用 iframe 的人,它不不适用于我。

\n

reactjs testing-library

7
推荐指数
1
解决办法
6425
查看次数

标签 统计

reactjs ×1

testing-library ×1