简体   繁体   English

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

[英]Check the input checkbox in a specific H3 text with Selenium Webdriver and Java

I want to check the input checkbox with selenium webdriver and Java, I am trying to click a checkbox inside a third heading (h3) input element of the page:- 我想使用Selenium Webdriver和Java来检查输入复选框,我试图单击页面的第三个标题(h3)输入元素内的复选框:

example of the element is:- 元素的示例是:-

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

I have tried the following method 我尝试了以下方法

//h3[starts-with(text(),'I want to click')]/input

and

//h3[text()='I want to click the checkbox for this particular text']/input

None of these seems to work. 这些似乎都不起作用。 Can you please suggest another possible method of doing this. 您能否建议另一种可行的方法。 Thanks. 谢谢。

Try using normalize-space() : 尝试使用normalize-space()

//h3[normalize-space(.)='I want to click the checkbox for this particular text']
    /input

or 要么

//h3[starts-with(normalize-space(.))='I want to click']/input

See http://www.w3.org/TR/xpath/#function-normalize-space 参见http://www.w3.org/TR/xpath/#function-normalize-space

The xpaths used in the question wont work,because input come first then comes the text . 问题中使用的xpath无效,因为inputinput然后input text

You can use the position of h3 element to check the correct option. 您可以使用h3元素的位置来check正确的选项。

for example if both the h3 elements are within a div, the following xpath would work. 例如,如果两个h3元素都在div内,则以下xpath将起作用。

//div[@id='divs_id']/h3[@class='header-element'][1]/input

The above xpath will click on first checkbox. 上面的xpath将单击第一个复选框。 similarly use the following xpath to click on second checkbox, 类似地,使用以下xpath单击第二个复选框,

//div[@id='divs_id']/h3[@class='header-element'][2]/input

check this fiddle 检查这个小提琴

is it something like this 是这样的吗

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM