2014-01-15 132 views
1

我在我的Selenium中遇到以下問題。它會生成錯誤,該元素不再附加到DOM。我發現有些解決方案需要等待,但我無法弄清楚是否可以等待沒有ID的元素。如果我只有className,我可以等待一個元素嗎?元素不再連接到DOM硒

require 'selenium-webdriver' 
#require Firefox installation !! 

browser = Selenium::WebDriver.for :firefox 
browser.get <URL> 

wait = Selenium::WebDriver::Wait.new(:timeout => 20) 

js_code = "return document.getElementsByClassName('Cell ')" 

rawdata = Array.new 
puts rawdata.size 

elements = browser.execute_script(js_code) 
elements.each{|e| rawdata.push(e.text) } 

puts rawdata.size 
arrSize = rawdata.length 


puts rawdata.at(5) + " " + rawdata.at(4) + " " + rawdata.at(9) + " " + rawdata.at(6) 

回答

1

這回答你的問題,但不一定能解決你的例外。如果沒有,您可能需要發佈HTML代碼片段和堆棧跟蹤。

這裏是如何在Ruby中使用WebDriverWait:

# create wait like you have already done 
wait = Selenium::WebDriver::Wait.new(:timeout => 20) 

# wait until something, you can use any locators you want, not just ids 
# don't inject JavaScript directly, unless you have to 
wait.until { driver.find_element(:class => "dojoxGridCell") } 

# do stuff to your raw data