English 中文(简体)
封顶-带XPath的钱财在范围上留下
原标题:capybara - Find with xPath is leaving the within scope

我正试图利用违约铁路日期、时间和日间田与Capybara建立日期选择。 我使用了within方法,为外地找到选定箱子,但当我使用xPath来寻找正确的箱子时,它便留下了>随附在<<>>/code>范围内,并在该元件的网页上发现了第一起。

这里是我使用的法典。 我正在对网页进行2个时段的测试,但我只能因这一错误而改变第1页。 此时,我有一只 d干的 container箱,清理了时日现场,但我确实计划改用标签找到该代码。

module  Marketron
  module DateTime

    def select_date(field, options = {})
      date_parse = Date.parse(options[:with])

      year = date_parse.year.to_s
      month = date_parse.strftime( %B )
      day = date_parse.day.to_s

      within("div##{field}") do
        find(:xpath, "//select[contains(@id, "_#{FIELDS[:year]}")]").select(year)
        find(:xpath, "//select[contains(@id, "_#{FIELDS[:month]}")]").select(month)
        find(:xpath, "//select[contains(@id, "_#{FIELDS[:day]}")]").select(day)
      end

    end

    def select_time(field, options = {})
      require "time"
      time_parse = Time.parse(options[:with])

      hour = time_parse.hour.to_s.rjust(2,  0 )
      minute = time_parse.min.to_s.rjust(2,  0 )

      within("div##{field}") do
        find(:xpath, "//select[contains(@id, "_#{FIELDS[:hour]}")]").find(:xpath, "option[contains(@value,  #{hour} )]").select_option
        find(:xpath, "//select[contains(@id, "_#{FIELDS[:minute]}")]").find(:xpath, "option[contains(@value,  #{minute} )]").select_option

      end

    end

    def select_datetime(field, options = {})
      select_date(field, options)
      select_time(field, options)
    end

    private

      FIELDS = {year: "1i", month: "2i", day: "3i", hour: "4i", minute: "5i"}

  end
end

World(Marketron::DateTime)
最佳回答

You should specify in the xpath that you want to start with the current node by adding a . to the start:

find(:xpath, ".//select[contains(@id, "_#{FIELDS[:year]}")]")

Example:

我测试了该网页的超文本(完全不简化你的网页):

<html>
    <div id= div1 >
        <span class= container >    
            <span id= field_01 >field 1</span>
        </span>
    </div>
    <div id= div2 >
        <span class= container >
            <span id= field_02 >field 2</span>
        </span>
    </div>  
</html>

Using the within methods, you can see your problem when you do this:

within("div#div1"){ puts find(:xpath, "//span[contains(@id, "field")]").text }
#=> field 1
within("div#div2"){ puts find(:xpath, "//span[contains(@id, "field")]").text }
#=> field 1

But you can see that but specifying the xpath to look within the current node (ie using .), you get the results you want:

within("div#div1"){ puts find(:xpath, ".//span[contains(@id, "field")]").text }
#=> field 1
within("div#div2"){ puts find(:xpath, ".//span[contains(@id, "field")]").text }
#=> field 2
问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...