Can't set radio button checked value with webbrowser control - html

I'm having trouble setting a radio button via a webbrowser control in VB.NET. This is the HTML code I'm trying to act upon:
<td align="right">
Afficher statuts :
</td>
<td>
<label for="Company">
<input type="radio" id="Services" value="true" name="requestinfo_0" />
Company
</label>
<label for="Partener">
<input type="radio" id="Workload" value="false" name="requestinfo_0" />
Partener
</label>
<label for="All">
<input type="radio" id="All" value="" name="requestinfo_0" />
All
</label>
The solution I found from searching shows it should work like this, but it doesn't!
WebBrowser1.Document.GetElementById("Workload").SetAttribute("Checked", True)
But I believe I would need to specify the name as well as from what I can tell it's possible to have more than 1 ID as "Workload" but in a radio button with a different name. But I can't see where or how to write the code accordingly.
I'm doing this in Visual Studio 2019 in Visual Basic.

You must to do that after your WebBrowser have finished to load the document. A little example below:
WebBrowser1.ScriptErrorsSuppressed = True
WebBrowser1.Navigate("https://html.com/input-type-checkbox/")
AddHandler WebBrowser1.DocumentCompleted, Sub(senderObj As Object, eObj As WebBrowserDocumentCompletedEventArgs)
Dim love As HtmlElement = WebBrowser1.Document.GetElementById("love")
If love IsNot Nothing Then
love.SetAttribute("checked", "checked")
End If
End Sub

Solved the problem by using a click event! Works fine now!
For Each curElement As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "Services" Then
curElement.SetAttribute("checked", "checked")
End If
If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "Radio3" Then
curElement.SetAttribute("checked", "checked")
End If
If curElement.GetAttribute("type") = "radio" AndAlso curElement.GetAttribute("id") = "all" Then
curElement.SetAttribute("checked", "checked")
End If
Next
Pete

Related

Html.CheckboxFor with clickable label not working

I am trying to implement a checkbox that has a clickable label with the Html.CheckboxFor syntax. However, I am running into an issue where my checkbox is not allowing me to click it to toggle its state and is not being set properly when the page is loaded even thought the checked attribute is correct.
From what I have been reading, it seems that this is do to the extra hidden input field that gets generated to pass back false values to the controller on a form submittal. However, I still need to figure out a way around this and I feel like I am not the first one to be looking for a solution.
Here is my HTML:
<div class="form-check">
#Html.CheckBoxFor(x => x.ProfilePublicViewable, new { #class = "form-check-input", #id = "val1", #value = "val1", #checked = "checked" })
<label class="form-check-label" for="val1">Profile Public Viewable</label>
</div>
And here is what gets rendered to the DOM:
<div class="form-check">
<input checked="checked" class="form-check-input" id="val1" name="val1" type="checkbox" value="val1">
<input name="ProfilePublicViewable" type="hidden" value="false">
<label class="form-check-label" for="val1">Profile Public Viewable</label>
</div>
I have been looking around for examples on how to achieve this design, but seem to be coming up empty handed. Does anyone have any ideas as to where i am going wrong?

Trying to fill in a form to login to a site via VBA but wont register the text I've entered

First time poster, long time user.
I'm trying to write a script in VBA to login to a site to then extract data at a later step.
I've hit a road block with trying to logon to the site, there is three classes that change once you manually fill in the username and password and two aria-invalid that turn from true to false. Unfortunately, this doesn't update if you try and fill in the forms via VBA.
This is the VBA code I currently have
Option Explicit
Sub LoginToSite()
Dim IE As New SHDocVw.InternetExplorer
IE.Visible = True
IE.navigate "https://website.com.au"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
IE.Document.forms("form").elements("username").Value = "FredFlinstone"
IE.Document.forms("form").elements("password").Value = "Password000"
IE.Document.forms("form").elements("submit-button").Click
End Sub
This is the HTML before manual text is added into the forms:
`<form name="form" class="login-form ng-pristine ng-invalid ng-invalid-required" novalidate="" autocomplete="off">
<div class="field-container">
<label for="username">
Username
</label>
<input name="username" class="ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" id="username" aria-invalid="true" required="" type="text" ng-keydown="vm.loginOnKeyEnter($event)" ng-class="vm.usernameEmptyClass" ng-model="vm.username">
</div>
<div class="field-container ">
<label for="password">
Password
</label>
<input name="password" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" id="password" aria-invalid="true" required="" type="password" ng-keydown="vm.loginOnKeyEnter($event)" ng-class="vm.passwordEmptyClass" ng-model="vm.password">`
These are the parts that change in the HTML if you manually add text.
Form div
<form name="form"
class="login-form ng-dirty ng-valid-parse ng-valid ng-valid-required"
novalidate=""
autocomplete="off">
Username Div
<input name="username"
class="ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required"
id="username"
aria-invalid="false"
required=""
type="text"
ng-keydown="vm.loginOnKeyEnter($event)"
ng-class="vm.usernameEmptyClass"
ng-model="vm.username">
Password Div
<input name="password"
class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched"
id="password"
aria-invalid="false"
required=""
type="password"
ng-keydown="vm.loginOnKeyEnter($event)"
ng-class="vm.passwordEmptyClass"
ng-model="vm.password">
Very keen to learn what I need to change in order to trigger the site into realising there is text within the boxes to be able to login.
I've still got my L plates on with VBA, so code will most definitely not be the most efficient code, but keen to hear of better methods to executing the above
Thanks in advance Mr Swan
Try to use the .Focus method to focus the TextBox first, then use the SendKeys statement to enter the value.
The below code should work if it's single page but you'll have to fill out the SignIn Div information portion since this doesn't appear to be a valid URL. Just follow the same format and note that everything is case sensitive. TKs.
Sub LoginToSite()
Const cURL = "https://website.com.au"
Const cusername = "FredFlinstone"
Const cpassword = "Password000"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UsernameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim SigninButton As HTMLInputButtonElement
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document
'Get the only form on the page
Set LoginForm = doc.forms(0)
'Get the Username textbox and populate it
'input name="username" id="username" size="18" value="" class="ng-touched ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required" type="text"
Set UsernameInputBox = doc.getElementById("username")
UsernameInputBox.Value = cusername
'Get the Password textbox and populate it
'input name="password" id="password" size="18" class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" type="text"
Set PasswordInputBox = doc.getElementById("password")
PasswordInputBox.Value = cpassword
'Get the form input button and click it
'button class="XXXXX" name="XXXXX" id="XXXXX" value="XXXXX" type="text"
Set SigninButton = doc.getElementById("XXXXX")
SigninButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
Is everything on one page or is it split between two pages like the Gmail login?
1 page example: enter username, enter password, click login
2 page example: enter username (click next), enter password, click login

How to fill html input with random tag id and name in vb.net?

I'm trying to fill a html input box, but it won't fill, because i can't get the value of the tag name or id, because it changes every time the page is reloaded, here is the html code:
<input id="1b9b8d81-aead-4930-90de-4c20121bc2d6.loginForm.username" maxlength="50" name="1b9b8d81-aead-4930-90de-4c20121bc2d6:username" value="" tabindex="1" class="mail inpRad" type="text" crmwa_forminfo="e-mail|">
And here is what I tried to do in vb.net:
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("value") = "mail inpRad" Then
webpageelement.SetAttribute("value", "loloolloolloollol")
End If
Next
It didn't work.. I need some clue to get it working.

Checkbox Value in VBscript/HTML

I have created a simple form that sends data to an Excel file with an array.
Everything works great except check boxes. What I would like is if the checkbox is "checked" it will post its value into the Excel document. Currently the value assigned for the box (checked) is always pasted no matter if it is checked or not. I am unsure what code is needed for the arr(6).
<script>
function PrintFunction() {
window.print();
}
</script>
<script type="text/vbscript">
function test()
dim oApp, oWB, arr
set oApp = CreateObject("Excel.Application")
oApp.visible=false
set wb = oApp.Workbooks.Open("C:\(PATH TO DB)\DB.xls",,,,"password")
redim arr(6)
arr(0) = Date()
arr(1) = Form1.text1.Value
arr(2) = Time()
arr(3) = Form1.text2.Value
arr(4) = Form1.text3.Value
arr(5) = Form1.text4.Value
arr(6) = Form1.text5.Value <---Need help with this value
with wb.sheets("Data").cells(1,1).currentregion
.offset(.rows.count,0).resize(1).value = arr
end with
wb.close true
x=msgbox("Submitted." ,64, "Thank you.")
window.close()
end function
</script>
</head>
<body oncontextmenu="return false;">
<form name="Form1">
<table border="0" style="width:700px;">
<td>Date:<input value="mm/dd/yyyy" name="text1" type="text" class="inputbox" size="20" />
<tr>
<td>Time:<input value="00:00 AM/PM" name="text2" type="text" class="inputbox" size="20" /></td>
<tr>
<td>Name:<input name="text3" type="text" class="inputbox" size="23"/></td>
<tr>
<td>Location:<input name="text4" type="text" class="inputbox" size="18" /></td>
<tr>
<td><input type="checkbox" name="text5" value="checked">Checkbox Title</td>
<input type="button" value="Print" class="classname" onclick="PrintFunction()" />
<input type="button" value="Submit" class="classname" onclick="test()" />
</table>
<br>
</form>
To name a checkbox "test5" is an atrocity. Just see what it did to Josh.
This
<html>
<head>
<hta:application id = "cbxproblem">
<script type="text/vbscript">
function test()
MsgBox Form1.cbx.checked ' <---Need help with this value
MsgBox Form1.cbx.value
end function
</script>
</head>
<body>
<form name="Form1">
<input type="checkbox" name="cbx" value="pipapo" checked="checked">Checkbox Title
<hr />
<input type="button" value="Submit" onclick="test()" />
</form>
</body>
</html>
is all the code needed to present/discuss your problem. Dragging Excel and the text elements into the question is obfuscation and wasting other people's time.
Reading about the checkbox element and comparing
<input type="checkbox" name="text5" value="checked">
vs.
<input type="checkbox" name="cbx" value="pipapo" checked="checked">
should make you realize the differences between the value and the checked attribute.
Running the demo code will prove that the checked attribute is set to True or False depending on the user (not) marking/checking the box.
See this to understand why
If Form1.text5.Checked = True Then ' <-- nonono
should be
If Form1.text5.Checked Then
The value of a text box is "Checked" or "Unchecked". If I understand your problem correctly, it sounds like there is another text box on the form whose value you want to copy (though I don't see it in your HTML code; maybe you need to add it?). Something like:
If Form1.text5.Checked = True Then
arr(6) = Form1.text6.Value 'Or .Text
End If

html method=get not passing values to the next aspx page

i have simple html page with 3 textboxes.
<form id="form1" method=get action="http://mysite.com/default.aspx" runat="server">
<div>
<input id="name" type="text" value="Amy" />
<input id="email" type="text" value="amy#jf.com"/>
<input id="phone" type="text" value="2125552512" />
</div>
<input id="Submit1" type="submit" value="submit" />
</form>
Now when it loads default.aspx i have this code in the vb backend on page_load.
Dim tbName As TextBox = Page.FindControl("Name")
Dim tbPhone As TextBox = Page.FindControl("Phone")
Dim tbEmail As TextBox = Page.FindControl("Email")
If page.request("name") & "" <> "" AndAlso tbname IsNot Nothing Then
tbname.text = page.request("name")
End If
If page.request("email") & "" <> "" AndAlso tbEmail IsNot Nothing Then
tbEmail.text = page.request("email") & ""
end If
If page.request("phone") & "" <> "" AndAlso tbphone IsNot Nothing Then
tbPhone.text = page.request("phone") & ""
End If
The page loads but is these textboxes are empty. what am i doing wrong?
If you want to be able to access those controls serverside, you'll need to add the runat="server" attribute to each of them.
Also, the TextBox type you're referencing is the ASP.NET control, which you aren't using. What you'd be using, once you add the runat="server" tags is HtmlInputText.
You can use the TextBox type by using the TextBox ASP.NET control instead of the <input> elements:
<asp:TextBox ID="name" runat="server" Value="Amy" />
If all your ASP.NET page is doing is processing the request from the form, then there's no need to reference any textbox or input controls - it won't be possible since they don't exist as ASP.NET controls. All you need to do is read the values from Request.QueryString.
If the intent is for the inputs to be visible and/or editable once they're on the ASP.NET page, I'd recommend moving the HTML form into your ASP.NET page.
it is not like this that webform functionate.
First, your input in your form needs to be server control: ex <asp:TextBox runat="server" id="name" Text="value" />
Then in your codebehind file you do not have to go through Page.FindControl("YourInput") but only this.YourInput.Text