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 && (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
You are trying to find element by its
Id
but in the methodGetElementById()
you are givingname
of element you need to find element by it'sId
like thisDo same for the rest of two.
Edit
You need to add
DocumentCompleted
event ofWebBrowser
. This event occurs when theWebBrowser
has finished loading the document.