元素不再附着在DOM硒上

时间:2014-01-15 08:01:10

标签: ruby selenium selenium-webdriver

我的Ruby中的Selenium存在以下问题。它会生成错误,该元素不再附加到DOM。我找到了一些等待的解决方案,但是我无法弄清楚是否可以等待没有ID的元素。如果我只有className,我可以等待一个元素吗?

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 个答案:

答案 0 :(得分: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