3

How can I add a specific line from my document.txt to YouTube comment field with specific id and then move to next line on my document.txt. The id of YouTube comment field is 'contenteditable-root'. I have created this code but the text that is been added on the YouTube comment field show in brackets for example ["Hello"]

Or in second example it shows nothing

Example 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

Example 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 file:

Hellooo !!
hi
Goodbye 
Goodnight 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Astrit Shuli
  • 619
  • 4
  • 20
  • From your question, it appears that your `browser.execute_script` line is inserting `files` or `comments`. And that your problem is setting `files` or `comments` correctly in your iteration. Is that correct? If so, I think it would be helpful if you could add a very small example of the text you are trying to work with to your question (using the `edit` button below the tags). – jvillian Aug 20 '19 at 14:55
  • @jvillian Ok i just editet my question , so the problem is that i cant add for example the first text in comment field on youtube with id 'contenteditable-root' . In first example that does work but it shows in this case ['Hellooo !!'] instead of just Hellooo !! without brackets – Astrit Shuli Aug 20 '19 at 15:27

2 Answers2

1

Assuming a whole bunch of other things here are correct, you're doing a very weird iteration, you should just use:

file = 'comments.txt'
File.readlines(file).each do |i|
  browser.execute_script("document.getElementById('contenteditable-root').innerHTML = '#{i}';")
end
smathy
  • 26,283
  • 5
  • 48
  • 68
  • I have tried this to but it does nothing .. Runtime.evaluate threw exception: SyntaxError: Invalid or unexpected token (Selenium::WebDriver::Error::UnknownError) – Astrit Shuli Aug 20 '19 at 15:35
  • Confused because you said that you had that part working: _"but the text that is been added on the youtube comment field show in brackets for example ["Hello"]"_ – smathy Aug 20 '19 at 15:55
  • yes but i want to show it without brackets .. the method you showed me doesnt work either – Astrit Shuli Aug 20 '19 at 16:35
  • Heh, so no syntax error after all ;) And no, my code doesn't add brackets, see: https://repl.it/repls/DeadUnhealthyCharmap – smathy Aug 21 '19 at 18:47
1

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.

Gijs P
  • 1,325
  • 12
  • 28