Selenium Locators:嵌套查询,对多个元素进行部分匹配

时间:2015-05-22 13:21:45

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

我正在寻找稳定的嵌套查询,以便找到"输入"字段使用"类型"或者" class"属性(使用cssSelecor或使用xpath)。

像: div类匹配(" panel-wrap")/ div类匹配(" panel-body")/ div类匹配(" panel-class")/输入类型=文本或输入类匹配(" text-field")

基本上是嵌套查询与类名或id上的部分匹配的组合。

<div id="12-ext-5" class="fff-panel-wrap-rr">
<div id="30-ext-6" class="ggg-panel-body-gg">
<div id="40-ext-9" class="ddd-panel-class-hh">
<input id="24-ext-9" class="grr-text-field-gen00" type="text" name="ext-comp-r">

任何帮助?

2 个答案:

答案 0 :(得分:1)

  

类似:div类匹配(&#34; panel-wrap&#34;)/ div类匹配(&#34; panel-body&#34;)/ div类匹配(&#34; panel-class&# 34;)/ input type = text或input class matches(&#34; text-field&#34;)

将上面的句子翻译成xpath会出现类似这样的内容(为了便于阅读而格式化):

//div[contains(@class, 'panel-wrap')]
/div[contains(@class, 'panel-body')]
/div[contains(@class, 'panel-class')]
/input[@type='text' or contains(@class, 'text-field')]

答案 1 :(得分:0)

这css有帮助吗?

input[class^='grr-text-field'][type='text'][name='ext-comp-r']

请注意,该类是部分匹配。我假设-gen00是动态添加的。

以下应该是您编写的css选择器的编写方式。但是,我不建议您像这样编写选择器

div[class*='panel-wrap']>div[class*='panel-body']>[class*='panel-class']>input[class*='text-field']