youtube - Null Value from GetElementById using C# -
i've gone through loads of asked questions haven't been able find solution problem.
my application video finder, user enters he/she looking in textbox , selects 1 of 3 websites(youtube,metacafe,screen.yahoo) find video.
ive got method each of choices when reaches getelementbyid method returns null value three. im going assume ive missed , thats why im having null result 3 methods.
firstly here 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 elements search bar(from youtube) i'm trying 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 id , name element both have given me same result.
not sure weather need other 2 methods seeing identical i'm going post them incase.
here 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 element im trying 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 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 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 taking time read it. /d
you trying find element id
in method getelementbyid()
giving name
of element need find element it's id
this
htmlelement search = doc.getelementbyid("masthead-search-term");
do same rest of two.
also element
null
if page hasn't loaded can access element after page hasloaded completely
.
edit
you need add documentcompleted
event of webbrowser
. event occurs when webbrowser
has finished loading 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); }
Comments
Post a Comment