未捕获的 DOMException:无法在“CommandLineAPI”上执行“$”:不是有效的选择器

Pra*_*ena 5 selenium xpath dom css-selectors selenium-webdriver

我正在使用 selenium 为我的网页编写自动化 UI 测试。我正在测试的网页上有一个元素:

<&lt input type="checkbox" id="screening_questions[0].multiple_choice[0]-dealbreakerField" value="on" style="position: absolute; cursor: inherit; pointer-events: all; opacity: 0; width: 100%; height: 100%; z-index: 2; left: 0px; box-sizing: border-box; padding: 0px; margin: 0px;>
Run Code Online (Sandbox Code Playgroud)

由于该元素具有 id 属性,因此我尝试使用其 id 值来定位它,但没有成功。

如果我在 chrome 控制台中搜索该元素为:

$('#screening_questions[0].multiple_choice[0]-dealbreakerField')
Run Code Online (Sandbox Code Playgroud)

我收到异常:未捕获的 DOMException:

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
Run Code Online (Sandbox Code Playgroud)

我认为根据它的 id 值可以很容易地找到它。你能建议这里有什么问题吗?

Deb*_*anB 1

这个错误信息...

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
Run Code Online (Sandbox Code Playgroud)

...意味着您采用的定位器策略不是有效的选择器

根据您共享的HTML,所需的元素是一个带有属性 as 的<input>标签,要使用该属性,您必须转义字符,并且可以使用以下选项之一:typecheckboxid.