【问题标题】:Uncaught DOMException: Failed to execute '$' on 'CommandLineAPI': not a valid selector未捕获的 DOMException:无法在“CommandLineAPI”上执行“$”:不是有效的选择器
【发布时间】:2018-10-14 07:14:47
【问题描述】:

我正在使用 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;>

由于元素有 id 属性,所以我尝试使用它的 id 值来定位它,但没有成功。

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

$('#screening_questions[0].multiple_choice[0]-dealbreakerField')

我得到了异常:未捕获的 DOMException:

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.

我认为根据它的 id 值定位它会很简单。您能否建议这里可能有什么问题?

【问题讨论】:

  • 特殊字符需要转义,或者使用getElementById()
  • @SLaks 感谢您的回复。哪些特殊字符是我需要转义的问题?

标签: selenium dom selenium-webdriver xpath css-selectors


【解决方案1】:

此错误消息...

Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.

...暗示您已适应的Locator Strategy 不是有效的选择器

根据 HTML,您共享所需的元素是 &lt;input&gt; 标记,type 属性为 checkbox 并使用 id 属性您必须转义 . 字符,并且可以使用以下任一选项:

  • cssSelector

    "input[id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][type='checkbox']"
    
  • xpath

    "//input[@id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][@type='checkbox']"
    

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 2022-06-17
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多