ruby-on-rails - 将 document.txt 中的特定行添加到具有特定 id 的注释字段,然后移至下一行?

标签 ruby-on-rails ruby watir

如何将我的 document.txt 中的特定行添加到具有特定 ID 的 YouTube 评论字段,然后移至我的 document.txt 中的下一行。 YouTube 评论字段的 ID 是“contenteditable-root”。我已经创建了这段代码,但是添加到 YouTube 评论字段中的文本显示在方括号中,例如 ["Hello"]

或者在第二个例子中它什么都不显示

示例 1:

file = 'comments.txt'
File.readlines(file).each do |i|
            files = [i]
            files.each { |val| 

browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{files}';")

}
end

示例 2:

line_number = 1 
loop do
comments = IO.readlines('comments.txt')[line_number-1]
browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{comments}';")
line_number += 1 
end

comment.txt 文件:

Hellooo !!
hi
Goodbye 
Goodnight 

最佳答案

假设这里的一大堆其他事情都是正确的,你正在做一个非常奇怪的迭代,你应该只使用:

file = 'comments.txt'
File.readlines(file).each do |i|
  browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{i}';")
end

关于ruby-on-rails - 将 document.txt 中的特定行添加到具有特定 id 的注释字段,然后移至下一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575901/

相关文章:

ruby-on-rails - 在 cygwin 中安装 Rails 时出现问题

ruby-on-rails - `require' : no such file to load -- mkmf (LoadError)

ruby - 将消息发送到 Ruby 中的全局缓冲区

dom - HTML xpath 树转储?使用 Ruby Watir

ruby-on-rails - 如何避免ActionController::InvalidCrossOriginRequest异常?

javascript - ruby rails : How to identify nested_form elements in javascript functions?

ruby - 如何打印不带括号或引号的数组元素?

ruby - 特殊字符大写

ruby - 我如何告诉 Watir 在表格单元格内的具体位置双击?

xpath - 循环遍历 Watir 中的一组 div