【问题标题】:retrieve text from <p> on landing page using ruby watir使用 ruby​​ watir 从登陆页面上的 <p> 检索文本
【发布时间】:2013-10-02 01:32:44
【问题描述】:

我必须从网页中检索文本并将其放在控制台上。 我无法从下面的这个 html 中获取文本。谁能帮我解决这个问题。

<div class="twelve columns">
<h1>Your product</h1>
<p>21598: DECLINE: Decline - Property Type not acceptable under this contract</p>
<div class="row">
</div>

我在 irb 上尝试了b.div(:class =&gt; 'twelve columns').exist?,它显示true

我试过这个 - b.div(:class =&gt; 'twelve columns').text,它返回的不是段落中的标题文本。

我尝试使用 -b.div(:class =&gt; 'twelve columns').p.text,它返回给我error - unable to locate element, using {:tag_name=&gt;"p"}

【问题讨论】:

  • 我的回答有帮助吗?

标签: ruby watir


【解决方案1】:

只需在您为我编写的示例中执行此操作:

browser.div(:class => 'twelve columns').p.text

您最好的办法是检查您的页面 css 是否实际提供了元素结构,以及它们是否正确嵌套。

【讨论】:

    【解决方案2】:

    我稍微修正了你的 HTML:

    <div class="twelve columns">
    <h1>Your product</h1>
    <p>21598: DECLINE: Decline - Property Type not acceptable under this contract</p>
    <div class="row"></div>
    </div>
    

    让我们做一个小例子:

    div = b.div(:class => 'twelve columns')
    

    元素枚举如下:

    div.elements.each do |e|
      p e
    end
    

    会做这样的事情:

    <Watir::HTMLElement ... # <h1>Your product</h1>
    <Watir::HTMLElement ... # <p>21598: DECLINE: Decline - Property Type not acceptable under this contract</p>
    <Watir::HTMLElement ... #<div class="row">
    

    如果您想从 DIV 中指定子元素 P,请执行以下操作:

    p = div.p
    

    p = div.element( :tag_name => 'p' )
    

    当得到P的文本时:

    p.text # >> 21598: DECLINE: Decline - Property Type not acceptable under this contract
    

    或者用你的单个字符串做事件:

    b.div(:class => 'twelve columns').p.text
    
    => "21598: DECLINE: Decline - Property Type not acceptable under this contract" 
    

    【讨论】:

    • 感谢您的回答 Chelios & Majiora。我会尝试处理它们,如果这对我有用,请告诉你
    • @user1344631 那么你得到了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多