I have a question, how to change HTML input value?
<select name="sys_lenght" aria-controls="systable" class>
<option value="20">Value 1</option>
<option value="40">Value 2</option>
<option value="80">Value 3</option>
</select>
I used this code
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
Elementz.setAttribute("value", "80")
End If
Next
But it doesn't change the input value, only the text "Value 3".
How can I solve this problem? Thanks
sys_lenght's value indicates which option with the specified value is selected. Thus if you set sys_lenght.value to "80" it will select Value 3.
To change the value of the currently selected option you have to get a reference to that first. You can do so by getting the selectedIndex of sys_lenght, and then get the specific item from that index.
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
'Get the index of the selected option.
Dim SelectedIndex As Integer = Integer.Parse(Elementz.GetAttribute("selectedIndex"))
If SelectedIndex >= 0 Then
'Get the option element from the resulting index.
Dim SelectedOption As HtmlElement = Elementz.Children(SelectedIndex)
SelectedOption.setAttribute("value", "80")
Else 'No option selected.
MessageBox.Show("No option selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Exit For 'We found what we were looking for; stop looping.
End If
Next
First you have to understand how html open and close tag, you forgot to close the html selection option open tag i mean the last greter sign > then use javascript query to find the value within the option example
WebControl1.ExecuteJavascript('document.querySelector('option [value='your value']').selected=true;")
hope its help?
Related
I run into a weird problem when trying to automate inserting data into a website. Specifically one input field, here is html piece of that website:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off">
When I will insert value into that field using this code and click on "Lookup" button:
Dim SE As MSHTML.IHTMLElement
Set SE = HTMLDoc.getElementById("masked_consumer")
SE.Value = "574844"
The page is telling me that I'm missing information.
After I will manually click on that field and enter that number, the underlying html code changes to:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off" real="574844">
New real property shows up.
Any ideas how I can solve that using VBA? I tried, click and focus, and nothing really works, until I use mouse to click on that field and manually enter that number.
Thank you!
Try to refer example below may help you to solve the issue.
Example:
Sub demo()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://example.com"
IE.Visible = True
While IE.Busy
DoEvents
Wend
Set TrackID = IE.document.getElementById("masked_consumer")
TrackID.Focus
Application.SendKeys ("(574844)"), True
Application.SendKeys ("{ENTER}"), True
End Sub
I ran into a similar issue one time, and activating .OnClick event of that field helped.
In your case it would be
SE.onclick
before setting the value.
There can also be some other events such as OnChange that may result in "no data entered" errors, when they are not triggered.
Good luck :)
On this internal website it allow me enter a data and depending on the data the radio box will be enable. How do I know when the radio box is enabled.
Enabled
input id="Answer1" name="Answer" type="radio" value="C"
Disabled
input id="Answer1" name="Answer" disabled="disabled" type="radio" value="C"
When it is disabled the getAttribute will return "disabled" but when it is enabled it will return run time error
Invalid use of Null (Error 94)
if I used
msgbox ie.document.all("Answer1").getAttribute("disabled")
I tried the following if statement and none will caught it when it is enabled
ie.document.all("Answer1").getAttribute("disabled") = ""
ie.document.all("Answer1").getAttribute("disabled") = Null
ie.document.all("Answer1").getAttribute("disabled") <> "disabled"
ie.document.all("Answer1").getAttribute("disabled") = "disabled"
Reference:HTML disabled Attribute
Disabled is a boolean attribute. This means that is value should not be set (e.g. disabled="disabled"). If disabled is present then the option is disabled.
<option value="volvo" disabled>Volvo</option>
You should be able to use msgbox ie.document.all("Answer1").disabled. If this doesn't work trap the error
Function isDisabled(ele As Object) As Boolean
On Error Resume Next
isDisabled = ele.getAttribute("disabled")
On Error GoTo 0
End Function
I also find this worked too in if statement
if isnull(ie.document.all("Answer1").disabled) then
msgbox "Radio box disabled"
else
msgbox "Radio box enabled"
end if
I a wondering if and how you would be able to block a certain element in the webbrowser.
For example there is a certain element on a website:
<div class="btn" id="button" value="ClickMe"/>
Will you be able to block or hide that specific element in the webbrowser?
There might be a certain ad, iframe or whatever its called that might just be in the way?
Thanks for the help I appreciate it.
double click on webbrowser1 designer you should be in WebBrowser1_DocumentCompleted add this line
'selector by division id
webbrowser1.Document.getElementById("button").innerHtml = ""
'selector spesific element with spesific attribute
<input id="_authentication_token" name="_authentication_token" type="hidden" value="11740686"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("input")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "11740686") then ele.innerHtml = ""
next
for your exemple :
<div class="btn" id="button" value="ClickMe"/>
dim mycol as htmlElementCollection = webbrowser1.document.getElementBytagname("div")
for each ele as Htmlelement in mycol
if (ele.getAttribute("value").tostring = "ClickMe" and ele.getAttribute("id").tostring = "button" ) then ele.innerHtml = ""
next
I want to select a dropdown value with vb.net webbrowser
<OPTION value=1>One</OPTION>
<OPTION value=2>Two</OPTION>
The values 1 & 2 are without the quotes
If the value is within quotes such as value = "1"
Then I can use the code
WebBrowser1.Document.GetElementById("ID").SetAttribute("Value", "1")
But it does not work for the above.
Thanks in advance.
Actually it does not matter if there is a quote or not. I created test code and it seems to be working.
For Each Frame As HtmlWindow In currentWindow.Frames
Dim btnElementCollection As HtmlElementCollection =
Frame.Document.GetElementsByTagName("Select")
For Each curElement As HtmlElement In btnElementCollection
Dim controlName As String = curElement.GetAttribute("id").ToString
If controlName = TextBox2.Text Then
curElement.SetAttribute("Value", TextBox3.Text)
End If
Next
Next
TextBox2 is the id TextBox3 is the value
Sorry, not sure why it didn't work the first time and thanks for everyones time.
Try setting the attribute name to uppercase.
<OPTION VALUE=1>One</OPTION>
<OPTION VALUE=2>Two</OPTION>
Note "value" is now "VALUE"
You should set id of the target element.
This should be
GetElementById("ID")
this
GetElementById("element id ")
View your page source to get the correct id.
I have to click a HTML button programatically which is on the 3rd page of the website . The button is without id. It has just name type and value . The HTML code of the button is given below
<FORM NAME='form1' METHOD='post' action='/dflogin.php'>
<INPUT TYPE='hidden' NAME='txtId' value='E712050-15'>
<INPUT TYPE='hidden' NAME='txtassId' value='1'><INPUT TYPE='hidden' NAME='txtPsw' value='HH29'>
<INPUT TYPE='hidden' NAME='txtLog' value='0'><h6 align='right'>
<INPUT TYPE='SUBMIT' NAME='btnSub' value='Next' style='background-color:#009900; color:#fff;'></h6>
</FORM>
i am using the following code to click it
For Each webpageelement As HtmlElement In allButtons
If webpageelement.GetAttribute("value") = "Start" Then
webpageelement.InvokeMember("click")
End If
Next
But i cant able to click it . I am using the vb.net 2008 platform. Can anyone tell me the solution to click it?
Try invoking Submit on the Form rather than click on the Input.
EDIT: Oops, HTMLElementCollection does not implement the generic IEnumerable. Try this instead:
Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
l_forms.Item(0).InvokeMember("submit")
End If
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
elem.InvokeMember("click")
Next
I spent quite a while trying to find an answer to this. I never realized that you can invoke a click using javascript. Once I read that the solution becomes very easy:
Public Function clickbyid(ByVal id)
If TheBrowser.Document.GetElementById(id) IsNot Nothing Then
Dim Headers As String = "" 'insert headers if you want to
TheBrowser.Navigate("javascript:document.getElementById('" & id & "').click();", "_self", Nothing, Headers)
'This keeps the function active until the browser has finished loading
Do While TheBrowser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Return 1
Else
MessageBox.Show("Could not find link by id" & id)
Return Nothing
End If
End Function
You may have to change "TheBrowser" to "WebBrowser1".
If you don't want to return a result then simply change it to public sub and remove the return statements.
If the htmlelement does not have an id then use the following functions to add one
Public Function clickbyelement(ByVal theButton As HtmlElement)
Try
'Generate a unique id to identify the element
Dim randomID = "vbAdded" & GetRandom(10000, 100000).ToString
'check to make sure the ID does not already exist
While TheBrowser.Document.GetElementById(randomID) IsNot Nothing
randomID = "vbAdded" & GetRandom(10000, 100000).ToString
End While
'add the ID to the element
theButton.SetAttribute("id", randomID)
'click
clickbyid(randomID)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
' by making Generator static, we preserve the same instance '
' (i.e., do not create new instances with the same seed over and over) '
' between calls '
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Thanks to Dan Tao for the random number: https://stackoverflow.com/a/2677819/381273
Tested and working!
It sounds like you are trying to do a client-side action - click the button when operating on the server (VB code). This will not work.
So, either use Javascript document.form1.submit() or call the VB sub that is connected to the button btnsub.click (or whatever you named it).
The Solution:
WebBrowser1.Navigate("UrlHere!")
'wait till the page is laoded
Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "NAME='btnSub'", "NAME='btnSub' id='btnSub'") 'insert id into youre button
WebBrowser1.Document.GetElementById("btnSub").InvokeMember("click") 'click on btnSub
;)