【问题标题】:Cannot set value for the <comment> and <p> element with Watir无法使用 Watir 为 <comment> 和 <p> 元素设置值
【发布时间】:2014-02-28 22:21:42
【问题描述】:

我正在为 Watir 使用并遇到为

设置值的问题

元素。

这是 HTML:

<div comment-editor-area="" class="comment-editor-area" contenteditable="true">
   <comment>    
       <p> "value goes in here"
          <br>
       </p>
 </comment>
</div>

我在使用时得到未定义的方法“set”:

 browser.element(:xpath, "//*[@id='comment-editor']/div/div/comment/p" ).set "Value"

谁能帮忙?

错误信息:

NoMethodError: undefined method `set' for #<Watir::HTMLElement:0x..fdae41aef064b3f16    located=false selector={:xpath=>"//*[@id='comment- editor']/div/div/comment/p"}>:Watir::HTMLElement  

【问题讨论】:

    标签: ruby watir-webdriver paragraph


    【解决方案1】:

    这里有 2 个解决方法。

    使用 send_keys:

    使用send_keys 似乎允许将密钥发送到div 元素:

    browser.div.send_keys 'Value'
    

    将文本添加到 div:

    <div comment-editor-area="" class="comment-editor-area" contenteditable="true">Value
      Value
      <comment>    
        <p> "value goes in here"
          <br>
        </p>
      </comment>
    </div>
    

    假设您打算替换现有值,您可以这样做:

    browser.div.send_keys [:control, 'a'], :delete, 'Value'
    

    这将给出以下内容:

    <div comment-editor-area="" class="comment-editor-area" contenteditable="true">
      Value<br>
    </div>
    

    不幸的是,在 p 元素上尝试相同的方法似乎不起作用。似乎该解决方案仅在将密钥发送到第一个 contenteditable 元素(而不是其子元素)时才有用。

    (注意:以上是使用 Firefox 测试的。您可能会从其他浏览器得到不同的结果。)

    使用 execute_script:

    另一种方法是通过 javascript 更改 p 元素的文本:

    paragraph = browser.element(:xpath, "//*[@id='comment-editor']/div/div/comment/p" )
    browser.execute_script('arguments[0].innerHTHML = "Value";', paragraph)
    

    这会给:

    <div comment-editor-area="" class="comment-editor-area" contenteditable="true">
      <comment>    
        <p>Value</p>
      </comment>
    </div>
    

    【讨论】:

    • 嗨贾斯汀,感谢您的帮助。我决定尝试第一种方法。虽然一开始我得到一个参数[0]不存在的错误,但我对这个错误进行了更多调查......并且......我找到了你写的一篇文章:) [link] jkotests.wordpress.com/2013/03/19/…
    • 所以我使用了这种方法并且它起作用了:codescript = code
    • 抱歉打错字了。现在已经修复了。
    猜你喜欢
    • 2016-10-12
    • 2023-03-22
    • 2013-12-06
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    相关资源
    最近更新 更多