2013-08-31 38 views
1

page.find_by_id(id)工作,但做page.assert_selector("tr##{id}") returns a Capybara :: ElementNotFound`可能的原因是什麼?水豚 - assert_selector(「tr#1234」)不起作用,但find_by_id(1234)確實

對於背景,我使用Poltergeist驅動程序爲Capybara

我的HTML的結構如下所示:

<tbody> 
    <tr id="1234"> 
    <td>Rico Jones</td> 
    <td><a href="/price_requests/5678">Price Request</a></td> 
    </tr> 
    <tr id="2345"> 
    <td>Rico Jones</td> 
    <td><a href=/price_requests/6789">Price Request</a></td> 
    </tr> 
</tbody> 

我已經證實,我的HTML是走出來用騷靈的page.driver.debug功能,並查看由測試所產生的實際HTML預期。

當我在測試中插入類似的東西時,我收到Capybara::Poltergeist::InvalidSelector錯誤消息The browser raised a syntax error while trying to evaluate the selector

lead = Lead.first 
assert_selector "tr##{lead.id}" 

這樣做時,我也得到一個錯誤:

lead = Lead.first 
within "tr##{lead.id}" do 
    click_on "Price Request" 
end 

但是,使用find_by_id作品:

lead = Lead.first 
find_by_id(lead.id).click_on("Price Request") 

根據我的水豚的理解,這不應該是這樣的。難道我做錯了什麼?

+0

您可能會看到不一致性,因爲ID不應以數字開頭:http://css-tricks.com/ids-cannot-start-with-a-number/。 –

+0

你是對的。更改'id'以一封信開頭解決了我的問題。我實際上得到了一個'Capybara :: Poltergeist :: InvalidSelector:瀏覽器在嘗試評估選擇器錯誤時引發了一個語法錯誤,這應該讓我失望。想創建一個我能接受的答案嗎? –

回答

2

這是因爲ID不應以數字開頭,因爲shown here

ID和名稱標記必須以字母開頭([A-ZA-Z]),並且可以是 後跟任意數量的字母,數字([0-9]),連字符( 「 - 」) , 下劃線(「_」),冒號(「:」)和句點(「。」)。

相關問題