1

I've gone through loads of already asked questions but I haven't been able to find a solution for my problem.

My application is a video finder, the user enters what he/she is looking for in a textbox and then selects from one of three websites(Youtube,Metacafe,Screen.yahoo) to find the video.

Ive got a method for each of the choices but when it reaches the GetElementByID method it returns a null value for all three. Im going to assume ive missed something and thats why im having this null result for all 3 methods.

Firstly here is the Youtube method

private void YouTube(String Input)
        {
            try
            {
                webBrowser1.Navigate("https://www.youtube.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("search_query");
                Search.SetAttribute("value",Input);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

Here is the elements for the search bar(from youtube) I'm trying to access.

<input id="masthead-search-term" autocomplete="off" autofocus="" onkeydown="if (!this.value &amp;&amp; (event.keyCode == 40 || event.keyCode == 32 || event.keyCode == 34)) {this.onkeydown = null; this.blur();}" class="search-term masthead-search-renderer-input yt-uix-form-input-bidi" name="search_query" value="" type="text" tabindex="1" placeholder="" title="Search" dir="ltr" spellcheck="false" style="outline: none;">

I've tried both the id and name from this element but both have given me the same result.

Not sure weather you would need the other two methods seeing as they are almost identical but i'm going to post them just incase.

Here is the metacafe element

private void Metacafe(String Input)
        {
            try
            {
                webBrowser1.Navigate("http://www.metacafe.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("searchText");
                //webBrowser1.Document.GetElementById("searchText").SetAttribute("value", Input);
                Search.SetAttribute("value", Input);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

and the element im trying to connect to.

<input value="Search Metacafe" type="text" accesskey="s" class="TextField " title="Search Metacafe" autocomplete="off" size="50" name="searchText" tabindex="1" id="SearchQuery">

Lastly the Yahoo method.

private void Yahoo(String Input)
        {
            try
            {
                webBrowser1.Navigate("https://screen.yahoo.com/");
                HtmlDocument Doc = webBrowser1.Document;
                HtmlElement Search = Doc.GetElementById("p");
                Search.SetAttribute("value", Input);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }

        }

And its element.

<input id="UHSearchBox" type="text" class="yucs_W(100%) Fz(18px)! O(n):f Fw(200)! Bxz(bb) M(0)! Py(4px)! Bdrs(0)! Bxsh(n)" style="border-color: rgb(117, 144, 245); opacity: 1;" name="p" aria-describedby="UHSearchBox" data-ylk="slk:srchinpt-hddn;itc:1;" data-yltvsearch="https://video.search.yahoo.com/search/" data-yltvsearchsugg="/" data-satype="mini" data-gosurl="https://search.yahoo.com/sugg/ss/gossip-us_ss/" data-pubid="112" data-appid="" data-maxresults="10" data-resize=" " data-rapid_p="2">

Thanks for taking the time to read it. /D

  • I think your task would be easier, if you'd use the provided APIs for Youtube and the other sites. Querying the sites and fill the search form is possible to, but if at any point the pages change, you will have to rewrite your application... Check the youtube API here: https://developers.google.com/youtube/v3/ and I'm sure the other sites have something simmiliar – crazy_crank May 31 '15 at 10:22

2 Answers2

1

You are trying to find element by its Id but in the method GetElementById() you are giving name of element you need to find element by it's Id like this

HtmlElement Search = Doc.GetElementById("masthead-search-term");

Do same for the rest of two.

Also this element will be null if page hasn't loaded properly you can only access this element after page has loaded completely.

Edit

You need to add DocumentCompleted event of WebBrowser. This event occurs when the WebBrowser has finished loading the document.

 private void YouTube(String Input)
    {
        try
        {
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            webBrowser1.Navigate("https://www.youtube.com/");

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Error");
        }

    }
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlDocument Doc = webBrowser1.Document;
        HtmlElement Search = Doc.GetElementById("search_query");
        Search.SetAttribute("value",Input);
    }
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • Hi again, thanks for all your help. This has solved my problem : ] Just need to find away to send the Input variable down to the new method. But once again thanks for all your help. –  Jun 01 '15 at 08:23
  • You can make a global variable and save its value and than use that variable in new method. – Mairaj Ahmad Jun 01 '15 at 08:51
0

I had a similar problem with youtube. None of the IDs were found by GetElementById. Looks like the problem is caused by the other browsers. Opera and Edge website tools show other class names then the "Document.Body.InnerHtml" contains. So just copied the "Document.Body.InnerHtml" into notepad and found the classnames i were searching.

For example to get file names of all songs in a playlist. Open that playlist and call:

Document.GetElementsByTagName("li") 
go through all items and get video name with:
HtmlElement.GetAttribute("data-video-title") 
Bob
  • 1