首页 > 解决方案 > Nightwatch 无法通过 id 定位元素,单词之间有空格

问题描述

我使用 Nightwatch 来自动化 UI 测试,大多数时候我使用一个简单的 ID 选择器,例如:

<div data-testid="Phone" class="styles__Row-sc-1w30zs4-0 styles__CanonicalRow-sc-1w30zs4-1 class="styles__StyledInput-n24pwb-1 fryvby" value=""></div>

我在 Nightwatch 中找到了这个元素:[data-testid=Phone] 它工作得很好,但是我在定位这个元素时遇到了问题:

<div data-testid="Date of birth" class="styles__Row-sc-1w30zs4-0 styles__CanonicalRow-sc-1w30zs4-1 ijIenC" xpath="1"><div class="styles__StyledLabelContainer-sc-33nmzi-11 houBGy"><label for="190" class="styles__StyledLabel-sc-33nmzi-3 bAvMbu">Date of birth</label></div></div> 

我尝试使用以下方法找到它:[data-testid=Date of birth] 但出现错误:“运行 .locateMultipleElements() 协议操作时出错:无效选择器:指定了无效或非法选择器”

我认为这是因为单词之间的空格可以帮助我找到这个元素吗?谢谢

标签: seleniumnightwatch.jsqa

解决方案


推荐阅读