1

I am trying to make a YouTube video uploader to upload a video with a single click. So far I am managing to get to the http://www.youtube.com/upload page, but I can not find a way to use the button to upload my video.

After a little research I have found out that the proper way to upload a file is uploadVid.SendKeys("C:\\video.flv");. So far I am at this point:

    //  IWebElement uploadVid = driver.FindElement(By.Id("start-upload-button-single"));
   //   IWebElement uploadVid = driver.FindElement(By.XPath("//*[@id=\"upload-prompt-box\"]/div[1]"));
  //    IWebElement uploadVid = driver.FindElement(By.XPath("//*[@id=\"start-upload-button-single\"]"));
        IWebElement uploadVid = driver.FindElement(By.ClassName("upload-drag-drop-description"));
        uploadVid.SendKeys("C:\\video.flv"); 

The lines I have commented out are what I have tried so far without any success. I keep getting error element not found .

I use C# Selenium WebDriver in VS2013, WPF.

A Petrov
  • 417
  • 1
  • 9
  • 23
  • 4
    This is unnecessary. YouTube has an API to upload videos: https://developers.google.com/youtube/v3/code_samples/java#upload_a_video ...you don't need Selenium *at all*. – Arran Mar 24 '14 at 22:49
  • @Arran, I do not want to use the API. – A Petrov Mar 25 '14 at 07:04
  • Why do you not want to use the API? It would be far simpler, less error-prone and more efficient to use the API rather than attempting to use Selenium for this purpose. Selenium is the fallback solution for when an API is not available or not adequate. – Tetrinity Mar 25 '14 at 15:22
  • @AntonioPetrov you are making *more* work for yourself by working with Selenium as opposed to the API. The main reason for this is simply put, Selenium doesn't handle file input dialogs - they are OS level dialogs which Selenium has no control over whatsoever. So the workarounds are all hacks, and you've just shown that they are not guaranteed to work in each case. Using YouTube's API means you can bypass all this and work with something that is guaranteed and tested enough to work 100% of the time. – Arran Mar 26 '14 at 15:17
  • Thank you @Arran and @Tetrinity for explaining what the YouTube API does. Unfortunately that does not answer my question. As I already said I do not want to use the API nor do I want to share my reasons for that. The file dialog can be "used" by using the `SendKeys()` command, unfortunately I have no idea where to "send the keys". – A Petrov Mar 26 '14 at 21:12
  • 1
    @AntonioPetrov, as I said, Selenium cannot handle file upload dialogs. YouTube uses HTML5 input fields, and Selenium doesn't handle HTML5 elements even in the slightest. As I said, Selenium cannot support this. Selenium won't work here. – Arran Mar 26 '14 at 22:49
  • @Arran, I see. If you'd like you can post this as an answer so I can select it as the best one. – A Petrov Mar 27 '14 at 07:15
  • 1
    @Arran your comment has unfortunately not aged well as we in 2019 are now limited to ~5 uploads / day with the official API. I've figured out how to use AppleScript to upload videos, but of course that requires the focus of one's entire computer and is not ideal. A "headless" Selenium would allow us to create a faux API, but that doesn't appear possible now either. – technoplato Oct 17 '19 at 19:14
  • @lustig, 5 years ago...I'm not surprised it isn't valid any more :) – Arran Oct 18 '19 at 14:01
  • @Arran Youtube api quota is very limited, because after 3 uploads it's already over. – Pedro Lobito Oct 22 '19 at 21:39
  • Which is it? 3 or 5? – Arran Oct 23 '19 at 22:20
  • As of May 2020, an uploaded video cannot be made public on youtube (unless one goes through the audit) ! – Farrukh Chishti May 02 '22 at 10:58

1 Answers1

0

5 years later...
Here's a python solution to upload a video to YouTube using selenium. Should be easy to implement in C#.

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(5) # Wait up 5 sec before throwing an error if selenium cannot find the element (!important)
driver.get("https://www.youtube.com/upload")
elem = driver.find_element_by_xpath("//input[@type='file']")
elem.send_keys("C:\\full\\path\to\\video.mp4"); # Window$
#elem.send_keys("/full/path/to/video.mp4"); # Linux

Notes:
1 - Be smart, go slowly but surely;
2 - YouTube max uploads per day is 50, but on the first day is 100;
3 - As of 2019, youtube api is limited to 5 video uploads (◔ _◔)

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268