是否可以在CSS Selector中为标记名设置正则表达式?

时间:2017-05-16 05:26:49

标签: selenium-webdriver css-selectors

我想使用带有h2h3标记的selenium webdriver查找元素。

是否可以为上述要求设置通用css选择器?

[tag^='h'][att^=str]类似的内容?

3 个答案:

答案 0 :(得分:2)

使用逗号分隔的选择器列表。对于您的情况,请使用h2, h3选择器。有关详细信息,请参阅W3C Recommendation

答案 1 :(得分:1)

对于属性,是的,你可以。但对于元素,您可以将逗号分隔为可能的值。 W3 CSS Selectors

Selector            Example              Description                                                                              CSS
element1,element2   h1,h2                Selects every <ul> element that are preceded by a <p> element                             3
[attribute~=value]  [title~=flower]       Selects all elements with a title attribute containing the word "flower"                 2
[attribute|=value]  [lang|=en]            Selects all elements with a lang attribute value starting with "en"                      2
[attribute^=value]  a[href^="https"]      Selects every <a> element whose href attribute value begins with "https"                 3
[attribute$=value]  a[href$=".pdf"]       Selects every <a> element whose href attribute value ends with ".pdf"                    3
[attribute*=value]  a[href*="w3schools"]  Selects every <a> element whose href attribute value contains the substring "w3schools"  3

答案 2 :(得分:1)

这不是问题的直接答案,但作为附加选项,您也可以使用exs.shutdown(); 来匹配这两个元素,方法是开始XPath字母:

"h"