使用Selenium Webdriver和Java检查特定H3文本中的输入复选框

时间:2014-01-10 11:54:35

标签: java selenium xpath selenium-webdriver

我想用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

这些似乎都不起作用。你能否提出另一种可行的方法。感谢。

3 个答案:

答案 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

请参阅http://www.w3.org/TR/xpath/#function-normalize-space

答案 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)

check this fiddle

是这样的

<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>