Html 如何在冒号属性名称中选择不带特定字符串的元素
我有两个类似的元素:Html 如何在冒号属性名称中选择不带特定字符串的元素,html,css,typescript,webdriver,protractor,Html,Css,Typescript,Webdriver,Protractor,我有两个类似的元素: <g transform="matrix"> <image xlink:href="data:image/png" width="48"> </g> <g transform="matrix"> <image xlink:href="specyfic" width="48"> </g> <g transform="matrix"> <image xlink:href=
<g transform="matrix">
<image xlink:href="data:image/png" width="48">
</g>
<g transform="matrix">
<image xlink:href="specyfic" width="48">
</g>
<g transform="matrix">
<image xlink:href="specyfic" width="48">
</g>
public static getEventIconOnMap: ElementFinder =
element.all(by.css('image:not([xlink\:href*=specyfic]')).last();
您的代码对我来说没有多大意义,也不知道如何测试它,但我认为下面的代码可以工作。尝试像这样转义角色:
<g transform="matrix">
<image xlink:href="data:image/png" width="48">
</g>
<g transform="matrix">
<image xlink:href="specyfic" width="48">
</g>
<g transform="matrix">
<image xlink:href="specyfic" width="48">
</g>
public static getEventIconOnMap: ElementFinder =
element.all(by.css('image:not([xlink\:href*=specyfic]')).last();
下面是一个CSS选择器,可以根据您的代码进行调整:
image:not([xlink\:href*=specyfic]) {
// your code
}
这是一个正在使用的小提琴-它不是您正在使用的确切代码,但我是作为css选择器编写的:我不知道您的问题是否就是这个问题,或者这只是在编写答案时的键入错误,但我注意到这里出了一些问题:
public static getEventIconOnMap: ElementFinder =
element.all(by.css('image[width="48"]:not(image[xlink\\:href*="specyfic"'))).last();
您错误地删除了:
字符,并且没有在字符串内关闭选择器,而是在引号外关闭了太多表达式,您需要将其重写为:
public static getEventIconOnMap: ElementFinder =
element.all(by.css('image[width="48"]:not(image[xlink\:href*="specyfic"])')).last();
此外,您不能在:not()
伪代码中组合多个选择器:您编写的:not(image[xlink\:href*=“specyfic”])
,但浏览器将无法解析该选择器,这是一个语法错误考虑到选择器已经将选择限制为仅在第一部分(
image[width=“48”]
)中的
元素,您的否定不需要确保标记名。因此,请将选择器更正为以下内容:
image[width="48"]:not([xlink\:href*="specyfic"])
以下是更正后的完整代码:
public static getEventIconOnMap: ElementFinder =
element.all(by.css('image[width="48"]:not([xlink\:href*="specyfic"])')).last();
最好给出HTML示例,这样我们就可以在localSo上进行尝试。因此,您希望选择其中不包含工作“specyfic”的每个图像。这是正确的吗?我不能帮你给出一个完整的答案,但从目前的答案来看,我认为你遗漏了一个关键的问题。您的标记是svg,可能嵌入在HTML页面中。这意味着标记是外来内容,并且应用XML名称空间。因此,在CSS中,这意味着使用管道字符而不是反斜杠冒号。因为声明名称空间前缀绑定看起来很难(可能不可能),所以我会尝试对所有带前缀的属性使用*。因此,您可以尝试类似于
image[width=“48”]:not([*|href*='specyfic'])
css替代方案很好,但请注意使用的选择器[attribute*=
与[attribute~=
。例如,specyficccc将作为匹配返回,因为*
选择器只查找一个值,~
查找一个词。我知道区别,但OP说“名称中没有'specyfic',因此它可以放在属性中的任何位置。