哪个xpath适用于html代码

问题描述 投票:0回答:2

我有一个单选按钮的html代码

<span id="ViewModel_515" class="gwt-RadioButton WPOW WCOW WJPW WAPW" aria-checked="true" aria-disabled="false" role="presentation" data-automation-id="radioBtn">
  <input id="gwt-uid-451" class="WLOW" type="radio" name="ViewModel_513" value="on" tabindex="0" checked="">
  <label id="ViewModel_515-label" class="WMOW" for="gwt-uid-451" data-automation-selected="true" data-automation-label="Smart Calculation Based on Events">Smart Calculation Based on Events</label>
</span>

我试着使用以下xpath:

//label[text()='Smart Calculation Based on Events']/preceding-sibling::input[@type='radio']

哪个不行。

java selenium xpath
2个回答
0
投票

根据您提供的HTML,您可以使用以下xpath

//input[@class='WLOW' and starts-with(@id,'gwt-uid-') and starts-with(@name,'ViewModel_')]

或者您可以按如下方式获得更多细化:

//span[@class='gwt-RadioButton WPOW WCOW WJPW WAPW' and starts-with(@id,'ViewModel_')]/input[@class='WLOW' and starts-with(@id,'gwt-uid-') and starts-with(@name,'ViewModel_')]

0
投票

有时,如果单击文本,则不会选中该复选框。尝试使用下面提到的一个。

//input[@type ='radio'] (or)
//input[@type ='radio' and starts-with(@id,'gwt-uid-')] 


/*If you have more than one checkbox, use value in an index as per selection */

 (//input[@type ='radio'])[index]
 (//input[@type ='radio' and starts-with(@id,'gwt-uid-')])[index]

例如,该页面有两个复选框,需要单击第二个复选框。

(//input[@type ='radio'])[2]
(//input[@type ='radio' and starts-with(@id,'gwt-uid-')])[2]
© www.soinside.com 2019 - 2024. All rights reserved.