【问题标题】:How to use Watir (PhantomJS) to select on HTML ID?如何使用 Watir (PhantomJS) 在 HTML ID 上进行选择?
【发布时间】:2015-08-31 15:16:15
【问题描述】:

我正在尝试使用 Watir (Phantom JS) 在文本字段中输入文本。但是,如果我不只是在班级上选择,我该如何选择文本字段的 ID?

browser.text_field(id: "message").set     # would this work? or do I need
browser.text_field(name: "#message").set? # Can I drop the "#" ? 

【问题讨论】:

    标签: ruby-on-rails ruby phantomjs nokogiri watir-webdriver


    【解决方案1】:

    假设您有以下 HTML:

    <input type="text" id="message" name="name">
    

    通过使用&lt;input&gt; 标记的属性作为定位器,您可以在text_field 中输入字符串。例如,您可以使用以下之一:

    b.text_field(id: "message").set "this is the message"
    b.text_field(name: "name").set  "this is the name"
    

    据我所知,set? 方法仅适用于复选框和单选按钮。如果要检索 text_field 中的字符串,可以使用value方法:

    b.text_field(id: "message").set "this is the message"
    puts b.text_field(id: "message").value
    #=> this is the message
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 2015-07-31
      • 2017-02-08
      • 2014-03-17
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多