我正在尝试从多个复选框中选择一个复选框,并且该复选框都没有任何种类的唯一标识符。此时,我们唯一可以区分它们的方法是选中复选框的标签。但是标签被放置为html结构中的同级结构,如下所示:
<div _ngcontent-c9="" class="row pb-2">
<div _ngcontent-c9="" class="form-group col-10 pl-0 mb-0 d-flex justify-contents-center">
<div _ngcontent-c9="" class="col-6">
<label _ngcontent-c9="" class="col ml-2"> Homebound Services: <br _ngcontent-c9="">
</label>
</div>
<div _ngcontent-c9="" class="col-6 pl-0 ">
<input _ngcontent-c9="" type="checkbox">
</div>
</div>
</div>
<div _ngcontent-c9="" class="row pb-2">
<div _ngcontent-c9="" class="form-group col-10 pl-0 mb-0 d-flex justify-contents-center">
<div _ngcontent-c9="" class="col-6">
<label _ngcontent-c9="" class="col ml-2"> Home-Based Services:
<br _ngcontent-c9="">
</label>
</div>
<div _ngcontent-c9="" class="col-6 pl-0 ">
<input _ngcontent-c9="" type="checkbox">
</div>
</div>
在此代码段中,我需要选中两个带有标签“ Homebound services”和“ Home-based Services”的复选框。以下是我构建动态xpath的失败尝试:
//div[@class="row pb-2"]//label[contains(text()," Homebound ESY Services: ")] and //input[type="checkbox"]
如何构建此xpath?另外,如果我们可以为相同的元素构建动态的CssSelector,该怎么办?
答案 0 :(得分:2)
要标识相应的复选框,您可以使用以下解决方案:
XPath for Homebound Services
:
"//label[contains(., 'Homebound Services:')]//following::input[1]"
XPath for Home-Based Services
:
"//label[contains(., 'Home-Based Services:')]//following::input[1]"