2014-01-07 172 views
1

我使用黃瓜,紅寶石和硒。我有以下HTML:獲得div元素與硒的文本

<html> 
<body> 
    <div class="breadcrumbtrail" id="breadcrumbtrail"> 
    home 
    </div> 
</body> 
</html> 

和下面的步驟定義

begin 
    puts "got to bct step" 
    bctValue = @driver.find_element(:class_name, "breadcrumbtrail").getText() 
    puts "got bctValue value of #{bctValue}" 
    assert(crumb == bctValue, "breadcrumbtrail #{bctValue}, not #{crumb}") 
    end 

我的輸出顯示步驟正在運行;我曾嘗試從div中獲取「innerText」的屬性,這似乎給了我div的名稱。

我試過find_element之後的各種東西 - 這個告訴我undefined method getText。我應該在ruby步驟定義中使用什麼來獲取該div內的文本?有沒有人有關於此文件的建議?我發現的唯一一條經過了數百條線,許多支持每種語言,這使得我很難找到我需要的東西。

回答

0

您正在使用紅寶石。 Ruby不使用camelCasing。

改變這一行:

bctValue = @driver.find_element(:class_name, "breadcrumbtrail").getText() 

bctValue = @driver.find_element(:class_name, "breadcrumbtrail").get_text 

另外,不要使用:CLASS_NAME如果你有一個ID ......

bctValue = @driver.find_element_by_id('breadcrumbtrail').get_text 
0

事實證明, Selenium :: WebDriver:Element的正確方法是「text()」,而不是getText()。現在我已經將其更改爲「文本」,它工作正常。