Null Value from GetElementById using C#

2019-08-15 05:55发布

问题:

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

回答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);
    }