CrazyThink
CrazyThink

Reputation: 49

Insert text to textbox in webbrowser

Ι am trying to insert text to textbox in web browser but Ι have problem to do that..

Ι am trying this code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("192.168.10.1")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WebBrowser1.Document.GetElementById("nmgp_arg_fast_search").Focus()
WebBrowser1.Document.GetElementById("nmgp_arg_fast_search").InnerText = TextBox2.Text()
End Sub

Following error accured :

Object reference not set to an instance of an object.

Ι am sure name of textbox is "nmgp_arg_fast_search", because Ι am trying that code on www.google.com and the text name is "q" and its work.

and this what i got from firebug (EDIT: if i inspect from mozilla firefox using firebug i can see all code)

<td class="scGridToolbarPadding" width="33%" valign="middle" nowrap="" align="left">
<script type="text/javascript">
var change_fast_top = "";
</script>
<input type="hidden" value="SC_all_Cmp" name="nmgp_fast_search">
<input type="hidden" value="qp" name="nmgp_cond_fast_search">
<script type="text/javascript">
var scQSInitVal = "";
</script>
<span id="quicksearchph_top">
<input id="SC_fast_search_top" class="css_toolbar_obj" type="text" alt="{watermark:'Pencarian cepat', watermarkClass:'css_toolbar_objWm', maxLength: 255}" onchange="change_fast_top = 'CH';" size="30" value="" name="nmgp_arg_fast_search" style="vertical-align: middle; height: 16px; padding-right: 17px; display: none;" maxlength="255">
<input id="SC_fast_search_top" class="css_toolbar_obj css_toolbar_objWm" type="text" alt="{watermark:'Pencarian cepat', watermarkClass:'css_toolbar_objWm', maxLength: 255}" onchange="change_fast_top = 'CH';" size="30" value="" name="sc_clone_nmgp_arg_fast_search" style="vertical-align: middle; height: 16px; padding-right: 17px;" maxlength="255">
<img id="SC_fast_search_close_top" onclick="document.getElementById('SC_fast_search_top').value = '';document.F0_top.nmgp_opcao.value='fast_search';document.F0_top.submit();" src="/cts2_pn/_lib/img/scriptcase__NM__qs_close.png" style="display: none; right: 5px; cursor: pointer;">
<img id="SC_fast_search_submit_top" onclick="scQuickSearchSubmit_top();" src="/cts2_pn/_lib/img/scriptcase__NM__qs_lupa.png" style="right: 5px; cursor: pointer;">
</span>

But when i see from webbrowser1 to much hidden code and i cant find element.. thats why i always get error..

i found the problem but still not resolve my problem..

Upvotes: 1

Views: 3893

Answers (2)

slister
slister

Reputation: 789

This is what you want to do:

Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete
    Application.DoEvents()
Loop

WebBrowser1.Document.GetElementById("SC_fast_search_top").SetAttribute("value", TextBox2.Text())

That will put the text from TextBox2 into the input box with the id "SC_fast_search_top" on the currently loaded document. In the WebBrowser so you will need to load the desired page first and make sure you don't do this until the page is done loading by using this

Let me know if you have any questions.

Upvotes: 1

Vladimir Potapov
Vladimir Potapov

Reputation: 2437

I think you need to find element by Name and not by Id.In text from firebug i see name="nmgp_arg_fast_search" not id="nmgp_arg_fast_search"

Upvotes: 0

Related Questions