The GeckoFX 60 browser I'm using in WPF has an evaluate script method that takes in javascript code (in a form of a string).
What I did:
document.getElementById('date').innerText
on the console gave me the information I needed(C#)
string videoDate = "";
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
js.EvaluateScript("document.getElementById('date').innerText", out videoDate);
}
NewProject.VideoDate = DateTime.Parse(videoDate);
Problem:
It was catching an error so I placed a break before parsing the string and found out that the videoDate string is null
What I expected:
I expected it to return the •Jan 30, 2008
the console showed when I entered the js code on the browser.
So far these lines of code are working for me (both on the console and my wpf app's GeckoBrowser) when getting other information from the YouTube video:
js.EvaluateScript("document.title", out videoTitle);
= gets the Video Title
js.EvaluateScript("document.URL", out videoId);
= gets the Video Url (which I then filter out to only get the video id in c#)
A few more things I've tried that didn't work:
A. Using GeckoElement and retrieving the browser's Document and its textContent
GeckoElement elem = YouTubeBrowser.Document.GetElementById("date");
videoDate = elem.textContent;
B. Using GeckoElement and retrieving the browser's DomDocument and its textContent
GeckoElement elem = YouTubeBrowser.DOMDocument.GetElementById("date");
videoDate = elem.textContent;
C. Changed innerText to textContent (based on another SO answer I saw that said firefox didn't understand innerText (which is weird since it worked on the console but I guess they added support for that later on) but rather uses textContent to retrieve the value)
string videoDate = "";
using (Gecko.AutoJSContext js = new Gecko.AutoJSContext(YouTubeBrowser.Window))
{
js.EvaluateScript("document.getElementById('date').textContent", out videoDate);
}
NewProject.VideoDate = DateTime.Parse(videoDate);
I think this has to do with the dynamic DOM on YouTube.
While I could not retrieve the value by element Id I found the same information on another Tag by class name:
_gfxBrowser.Document.GetElementsByClassName("watch-time-text")[0].TextContent
Returns: "Published on Jan 25, 2019"