2014-01-10 59 views
1

我想使用selenium webdriver和Java檢查輸入複選框,我試圖單擊頁面的第三個標題(h3)輸入元素中的複選框: - 元素的使用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

請解釋一下你的問題,數據不充分或混亂 – Lijo

+0

我喜歡它,當開發商把聽衆複選框標籤上,這樣如果我點擊標籤,它切換複選框,然後我可以用硒點幾下不同的方法。 – djangofan

回答

1

在問題中使用的xpaths將無法正常工作,因爲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 
+0

當您想單擊100 h3元素列表中的特定複選框時無法正常工作。 Paul的答案是有效的。 – Petsome

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

我的意思是使用webdriver和xpath搜索複選框 – Petsome

2

嘗試使用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

+0

感謝Paul,normalize-space()修復了它 – Petsome

相關問題