哪个xpath适用于html代码

时间:2018-03-05 20:09:33

标签: java selenium xpath

我有一个单选按钮的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']

哪个不起作用。

2 个答案:

答案 0 :(得分: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_')]

答案 1 :(得分:0)

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

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

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

EXCEPTION CAUGHT BY GESTURE
The following assertion was thrown while handling a gesture:
Could not find a generator for route "/listadecompras" in the _MaterialAppState.