2016-01-22 19 views
0

我是新手在這裏。請指教。如何選擇複選框在我的情況?如果沒有名稱,標識或標籤文字,如何在水豚的複選框?

<ul class="phrases-list" style=""> 
<li> 
<input type="checkbox" class="select-phrase"> 
<span class="prase-title"> Dog - Wikipedia, the free encyclopedia </span> 
<a href="en.wikipedia.org">(en.wikipedia.org)</a> 
<div class="prase-desc hidden">The domestic dog (Canis lupus familiaris or Canis familiaris) is a domesticated...</div> 
</li> 

下,我不工作:

When /I check box "([^\"]+)"$/ do |label| 
     page.check(label) 
end 

步:我複選框 「狗 - 維基百科,自由的百科全書」

回答

0

如果你可以改變HTML,包標籤元素中的輸入和跨度

<ul class="phrases-list" style=""> 
<li> 
    <label> 
    <input type="checkbox" class="select-phrase"> 
    <span class="prase-title"> Dog - Wikipedia, the free encyclopedia </span> 
    </label> 
    <a href="en.wikipedia.org">(en.wikipedia.org)</a> 
    <div class="prase-desc hidden">The domestic dog (Canis lupus familiaris or Canis familiaris) is a domesticated...</div> 
</li> 

它還具有點擊「狗 - 維基百科...「文本也觸發複選框。隨着這一改變,你的腳步應該像書面一樣工作如果你不能修改html,那麼事情會變得更加困難。
喜歡的東西

find('span', text: label).find(:xpath, './preceding-sibling::input').set(true) 

應該工作,雖然我很好奇你是如何使用這些複選框從JS什麼也沒有把他們綁任何具體數值

+0

謝謝!我只是想知道是否有一些解決方法(不更改HTML)。但最好能夠肯定地改變。 – Tatyana

0

讓我們假設你是從改變HTML防止。在這種情況下,通過XPath查詢元素可能最容易。例如:

# Here's the XPath query 
q = "//span[contains(text(), 'Dog - Wikipedia')]/preceding-sibling::input" 

# Use the query to find the checkbox. Then, check the checkbox. 
page.find(:xpath, q).set(true) 

好的 - 它沒有它看起來那樣糟糕!我們來分析一下這個XPath,所以我們可以理解它在做什麼:‘元素當然,也有可能是很多‘

//span 

這第一部分說:「搜索整個HTML文檔,發現所有的’跨跨度’元素在HTML文檔中,因此我們需要限制這一點:

//span[contains(text(), 'Dog - Wikipedia')] 

現在我們只搜索包含文本的「跨度」要素「的狗 - 維基百科」據推測,本文將唯一標識頁面上所需的「span」元素(如果不是,則只需搜索更多的文本)。

此時,我們有與要求的「輸入」元素相鄰的「span」元素。因此,我們可以使用「preceding-sibling ::」來查詢「輸入」元素。XPath Axis

//span[contains(text(), 'Dog - Wikipedia')]/preceding-sibling::input 
相關問題