Seems to me like you need to learn how to debug.
Step 1: Check in irb
(about irb) if your code is printing the correct text:
File.readlines('comments.txt').each do |line|
p line
end
Expected output:
=> "Line 1"
=> "Line 2"
=> "Line 3"
If not, then look up how to read a file per line.
Step 2: Does your piece of Javascript actually work?
Go to the page you're trying to test, Open the debugger (F12) and run your Javascript directly from the console:
document.getElementById('contenteditable-root').innerHTML = 'hi';
If it doesn't work, then try learning more abut Javascript on how it works interacting with elements.
Step 3: Does my piece of code actually work from Watir?
Open up irb
again and try it out
require 'watir'
b = Watir::Browser.new
b.goto 'https://youryoutubepage.com/path'
b.execute_script("document.getElementById('contenteditable-root').innerHTML = 'hi';")
If it fails, google the error, look for Watir and execute_script.
Then finally run the whole combination of your code in irb
:
require 'watir'
b = Watir::Browser.new
b.goto 'https://youryoutubepage.com/path'
File.readlines('comments.txt').each do |line|
b.execute_script("document.getElementById('contenteditable-root').innerHTML = 'hi';")
sleep 5 # Give yourself some time to visually confirm the changes.
end
A quick Google about your SyntaxError: Invalid or unexpected token (Selenium::WebDriver::Error::UnknownError)
I see it might be a problem with the quotations that execute_script
doesn't like.
Maybe try reversing the quotes:
b.execute_script('document.getElementById("contenteditable-root").innerHTML = "hi";')
In the future, please try to pinpoint your problem and don't use StackOverflow as a place to debug your code. Get your code to work step by step and focus your question on a specific function that's not working as expected.