哪个元素给出了StaleElementReferenceError?

时间:2014-09-22 16:09:04

标签: ruby dom selenium selenium-webdriver

所以我的代码给了我[remote server] resource://fxdriver/modules/web-element-cache.js:8325:24:in `fxdriver.cache.getElementAt': Element is no longer attached to the DOM (Selenium::WebDriver::Error::StaleElementReferenceError)

代码中使用了几个元素,我试图看看哪个元素给了我这个错误,所以我可以确保它有一个wait

修改 这是代码:

path = [".//*[@id='sub_nav_content']/table/tbody/tr[2]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[3]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[4]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[5]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[6]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[7]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[8]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[9]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[10]/td[3]/a", ".//*[@id='sub_nav_content']/table/tbody/tr[11]/td[3]/a"]
path.each do |path|
    begin
        wait.until {
            element = browser.find_element(:xpath => path)
            element if element.displayed?
        }
        browser.find_element(:xpath => path).click
        table = wait.until {
            element = browser.find_element(id: "possible_matched")
            element if element.displayed?
        }
        if table 
            puts "Table Found"
        else 
            puts "Table Error"
        end
        #creates an 2D array containing patient name, admit date and prints to screen
        names = browser.find_elements(:xpath => ".//*[@id='possible_matched']/table/tbody/tr/td[1]")
        name_array = []
        names.each { |name| name_array << name.text}
        admits = browser.find_elements(:xpath => ".//*[@id='possible_matched']/table/tbody/tr/td[5]")
        admit_array = []
        admits.each { |date| admit_array << date.text }
        name_admit_array = name_array.zip(admit_array)
        name_admit_array.each do |name, date|
            puts "#{name}: #{date}"
        end
        #finds the location of the sub-array containing patient name and collection associated admit date
        patient_name = browser.find_element(:xpath => ".//*[@id='dialog-modal-cancel-hl7-preview']/table/tbody/tr[2]/td[1]").text
        collected_date = browser.find_element(:xpath => ".//*[@id='dialog-modal-cancel-hl7-preview']/table/tbody/tr[2]/td[4]").text
        puts patient_name
        puts collected_date
        mo, da, yr = collected_date.split('/').map(&:to_i)
        cd = [yr, mo, da]
        bl = name_admit_array.each_with_index.select { |(name, date), i|
            m, d, y = date.split('/').map(&:to_i)
            dt = [y, m, d]
            name.downcase == patient_name.downcase and (dt <=> cd)<0
        }.map {|x, i| i }

        blf = name_admit_array.values_at(*bl)
        if bl.any?
            bf = blf.rindex(blf.max) + 2
            wait.until {
                element = browser.find_element(:xpath => ".//*[@id='possible_matched']/table/tbody/tr[#{bf}]/td[6]/div/a")
                element if element.displayed?
            }
            browser.find_element(:xpath => ".//*[@id='possible_matched']/table/tbody/tr[#{bf}]/td[6]/div/a").click
        else
            browser.find_element(:xpath => "html/body/div[6]/div[1]/a/span").click
        end
    end while bl.any?
end

1 个答案:

答案 0 :(得分:0)

检查错误的完整堆栈跟踪,它将指向引发异常的确切行。