我想用selenium webdriver和Java检查输入复选框,我试图点击页面第三个标题(h3)输入元素内的复选框: -
元素的例子是: -
<h3 class="header-element">
<input type="checkbox" checked=""/>
I want to click the checkbox for this particular text
</h3>
<h3 class="header-element">
<input type="checkbox" checked=""/>
I dont want to click the checkbox for this text
</h3>
我尝试过以下方法
//h3[starts-with(text(),'I want to click')]/input
和
//h3[text()='I want to click the checkbox for this particular text']/input
这些似乎都不起作用。你能否提出另一种可行的方法。感谢。
答案 0 :(得分:3)
尝试使用normalize-space()
:
//h3[normalize-space(.)='I want to click the checkbox for this particular text']
/input
或
//h3[starts-with(normalize-space(.))='I want to click']/input
答案 1 :(得分:1)
问题中使用的xpath不起作用,因为input
先出现text
。
您可以使用h3元素的位置check
正确的选项。
例如如果两个h3元素都在div中,则以下xpath将起作用。
//div[@id='divs_id']/h3[@class='header-element'][1]/input
上面的xpath将点击第一个复选框。 同样使用以下xpath点击第二个复选框,
//div[@id='divs_id']/h3[@class='header-element'][2]/input
答案 2 :(得分:0)
是这样的
<h3 class="header-element">
<input type="checkbox" checked=""/>
I want to click the checkbox for this particular text
</h3>
<h3 class="header-element">
<input type="checkbox" />
I dont want to click the checkbox for this text
</h3>