selenium - 未捕获的 DOMException:无法在“CommandLineAPI”上执行“$”:不是有效的选择器
问题描述
我正在使用 selenium 为我的网页编写自动化 UI 测试。我正在测试的网页上有一个元素:
<< 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 值定位它会很简单。你能建议这里有什么问题吗?
解决方案
此错误消息...
Failed to execute '$' on 'CommandLineAPI': '#screening_questions[0].multiple_choice[0]-dealbreakerField' is not a valid selector.
...暗示您采用的Locator Strategy不是有效的选择器。
根据您共享的HTML ,所需元素是一个带有属性的<input>
标签as并且要使用您必须转义字符的属性,您可以使用以下任一选项:type
checkbox
id
.
选择器:
"input[id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][type='checkbox']"
路径:
"//input[@id=\"screening_questions[0].multiple_choice[0]-dealbreakerField\"][@type='checkbox']"
推荐阅读
- mongodb - 将文件保存和读取到 GridFS
- autohotkey - AutoHotkey 发送 {Del} 在 Win10 的资源管理器中不起作用
- python - Weasyprint 在调用 write_pdf 时获得未定义的属性:“AttributeError:'PosixPath' 对象没有属性'read_text'”
- r - 如何从字符串中提取数字,包括数字之前的所有文本
- php - 如何在 WordPress 中将数据库表中的每条记录注册为简码
- grafana - 限制对特定 CloudWatch 仪表板的访问
- gradle - 如何从命令行配置 JaCoCo Gradle 插件
- cordova - TypeError: Object(...) is not a function at AndroidPermissions.requestPermission
- objective-c - UIWebView 在尝试打开另一个应用程序的 URL 时显示“无法显示 URL”,但它在 Safari 中有效
- wix - 如何在重大升级期间使用 Wix 工具集显式删除 dll