How to usethe upload button in YouTube using Selenium WebDriver C#

余生长醉 提交于 2020-01-05 12:34:30

问题


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.


回答1:


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 (◔ _◔)



来源:https://stackoverflow.com/questions/22621232/how-to-usethe-upload-button-in-youtube-using-selenium-webdriver-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!