regex - 为什么 Selenium 在连字符之前插入反斜杠?
问题描述
以下代码在IE和Firefox中失败。Chrome 从来没有遇到过问题。
foundElement = driver.FindElement(By.Id("btn-GDR"));
它说找不到元素#btn\-GDR
为什么Selenium\
在之前插入 a -
?
火狐 65.0.2 版本 IE 11.0.9600.19301
编辑:更多信息:我试过使用
"btn\x2dGDR" 表示 \x2d 是 "-" 符号(十六进制中的 ASCII),但它不能解决问题。它总是在它前面插入一个“\”。
解决方案
As Selenium converts the different Locator Strategies into it's effective CSS selectors as per the switch - cases the values of class name, id, name, tag name, etc are converted through:
cssEscape(value);
The cssEscape(value)
is defined as:
private String cssEscape(String using) {
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
using = "\\" + Integer.toString(30 + Integer.parseInt(using.substring(0,1))) + " " + using.substring(1);
}
return using;
}
Hence you see the -
character being escaped by the \
character.
推荐阅读
- python - 哪个更适合映射值:重用映射函数或构建引用表进行查找?
- google-apps-script - 谷歌表单中的文件上传
- android - 是否可以在颤动中使用我们的自定义键盘而不是系统键盘?
- c# - ASP .Net Core/Razor 页面:工作时保持会话活跃
- selenium - 让 Selenium Hub 使用第一个可用节点而不是循环?
- html - 如何使用媒体查询使多个列表折叠
- python - Python原始套接字更改符号
- r - 在 R 中使用 LpSolveAPI 包对二进制整数编程模型执行敏感性分析时出错
- mysql - mysql根据给定的范围执行速率
- django-forms - 如何在 django 视图中选择和保存外键对象