代码之家  ›  专栏  ›  技术社区  ›  ryanthescot

如何访问此元素

  •  1
  • ryanthescot  · 技术社区  · 15 年前

    我正在使用Watir为一个web应用程序编写一些测试。我需要从下面的HTML中获取文本'Bishop',但是我不知道怎么做。

    <div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;">
       <div class="workprolabel wpFieldLabel">
        <span title="Please select a courtesy title from the list.">Title</span>&nbsp;<span class="validationIndicator wpValidationText"></span>
       </div>
       <span class="wpFieldViewContent" id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value"><p class="wpFieldValue ">Bishop</p></span>
      </div>
    

    html/body/form/div[5]/div[6]/div[2]/div[2]/div/div/span/span/div[2]/div[4]/div[1]/span[1]/div[2]/span/p/text()
    

    但是我不能用xpath格式化元素来获取它。

    5 回复  |  直到 15 年前
        1
  •  1
  •   pjd    10 年前

    如果段落是唯一的,您应该能够立即访问该段落:

    my_p = browser.p(:class, "wpFieldValue ")
    my_text = my_p.text
    

    HTML Elements Supported by Watir

        2
  •  0
  •   pablochan    15 年前

    尝试

    //span[@id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']//text()
    

    也许这会管用

    path = "//span[@id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']/p";
    ie.element_by_xpath(path).text
    

    并检查跨度的id是否恒定

        3
  •  0
  •   katmoon    15 年前

    也许你的名字后面还有个空格?

    <p class="wpFieldValue ">
    
        4
  •  0
  •   Željko Filipin    15 年前

    试试这些(为我工作,请注意后面的空格) wpFieldValue

    browser.p(:class => "wpFieldValue ").text
    #=> "Bishop"
    
    browser.span(:id => "dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value").text
    #=> "Bishop"
    
        5
  •  0
  •   Flexo - Save the data dump sunny moon    13 年前

    似乎在运行时,DIV样式将NONE更改为BLOCK。

    所以在这种情况下,我们需要收集文本(整个源代码或DIV源代码),并从文本中收集值

    例如:

    text=ie.text
    
    particular_div=text.scan(%r{div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;(.*)</span></div>}im).flatten.to_s
    
    particular_div.scan(%r{ <p class="wpFieldValue ">(.*)</p> }im).flatten.to_s
    

    上面的代码是一个将解决您的问题的示例。