使用Ruby Watir和PhantomJS定位元素的错误

时间:2014-01-07 19:40:07

标签: ruby phantomjs watir

我有一个在ChromeWebDriver中完美运行但在PhantomJS上失败的脚本。当我检查元素是否存在时,我收到以下错误:

  

[错误 - 2014-01-07T19:31:55.878Z] WebElementLocator - _handleLocateCommand - El   未找到的东西:GAVE UP。搜索停止时间:1389123115867

随着脚本的继续,这似乎不是一个问题。但是,稍后在脚本上将无法找到以下元素:

  

question.div(:class =>“choices”)

此特定脚本访问包含测试问题的页面。它们是随机顺序的。脚本决定了它是什么样的问题,并选择一个随机的答案。

感谢您的帮助。以下是相关代码:

def QuestionType(question)
    if question.div(:class => "questionPrompt").text_field.exists?
        puts "FITB"
        FITB(question)
    #elsif question.div(:class => "choices").ul(:class =>"choices-list").li(:index => 1).checkbox.exists?
    elsif question.checkbox.exists?
        puts "Checkbox"
        Checkbox(question)
    else
        puts "Radio"
        Radio(question)
    end

end

def FITB(question)
    arn = Random.new.rand(0..10)
    if arn == 0
        answers.li(:index => arn).radio.set
    else
        idx = 0
        begin
            question.div(:class => "questionPrompt").text_field(:index => idx).set("Test #{idx}")
            idx = idx + 1;
        end while question.div(:class => "questionPrompt").text_field(:index => idx).exists?
    end
    puts "FITB Complete"
end

def Checkbox(question)
    allAnswers = question.div(:class => "choices")
    answers = allAnswers.ul

    max = answers.lis.length - 1
    arn = Random.new.rand(0..max)
    if arn == 0
        answers.li(:index => arn).radio.set
    else
        for i in 1..arn
            answers.li(:index => i).checkbox.set
        end
    end
    puts "Checkbox Complete"
end

def Radio(question)
    allAnswers = question.div(:class => "choices")
    answers = allAnswers.ul

    max = answers.lis.length - 1
    arn = Random.new.rand(0..max)

    answers.li(:index => arn).radio.set

    puts "Radio Complete"
end

0 个答案:

没有答案