Getting Visual Basic Program to Type into an HTML <p> and <input> tag with no id/name - vb.net

So I have a program that takes a few inputs from textboxes and then adds them all up into two strings. Basically here is the problem, I am trying to get one of the strings to go into the forum post "title" and one into the forum "body". (This is not a spam program, it makes it easier for people to post ban reports for players on our server.) Here is the HTML codes for the website I am trying to type into;
<p class="ipsField_content">
<input id="topic_title" class="input_text" type="text" size="60" maxlength="150" name="TopicTitle" value="" tabindex="2">
</p>
The HTML code above is for the topic title area I want to type into. The problem with this is that it always says how it can not find this area.
'This is the HTML code for the forum body:
<body spellcheck="true" class="cke_show_borders">
<p>This is where I want to be able to type</p>
</body>`
With the HTML code above, I can't seem to get this VB code below to select that text area and enter in my string.
WebBrowser1.Document.GetElementsByTagName("p").InnerText = post
One reason is because VB doesn't allow me to use .innertext on a tag, and the other is that I do not know how to really specify which "p" tag. Like there is multiple "p" tags on this webpage, but it is the only "p" inside the "body" tag, if I could specify that somehow.
Here is the VB code I am using currently for the program.
Dim Report As String
Report = YourName & Suspect2 & Server & Time2 & Reason & RulesBroken & Proof
Dim topictitle As String
topictitle = ("" & Suspect & " - " & txtReason.Text & txtReasonCustom.Text & "")
txtTitle.Text = topictitle
txtPost.Text = Report`
making sure that the words "sign out" is on the page to insure the webbrowser is signed in.
`Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If WebBrowser1.DocumentText.Contains("Sign Out") Then
WebBrowser1.Navigate("www.ThisLinksToMakeNewPostOnTheWebsite.com")
WebBrowser1.Document.GetElementById("topic_title").InnerText = txtTitle.Text
WebBrowser1.Document.GetElementByTagName("body").firstchild.InnerText = txtPost.Text
End If`
this code below is used to click the submit button
`WebBrowser1.Document.Forms(0).InvokeMember("submit")`
also this is the html code for the "Post New Topic" button
<input type="submit" name="dosubmit" value="Post New Topic" tabindex="50" class="input_submit" accesskey="s">
Anyways that is it. I tried to include as much information and as much lines of code as I could to show that I really am trying.
Please help, I have tried the MSDN links, but I can't seem to apply it to this.

From your code I think you are using the WebBrowser control. There is a much less resource hogging alternative. Since you are really only interested in posting to a form, you don't need the overhead of a browser. You can use the HTTPWebRequest to POST the data/text from your textboxes directly to a URL. An example can be found here:
http://howtostartprogramming.com/vb-net/vb-net-tutorial-51-httpwebrequest-post-method/
It's not the best tutorial in the world but it will get you on your way. You would of course still need to know what data you need to send to the web site.

Related

“Click” a javascript button with VBA

I am trying to use VBA to click the button: search job posting
button
The html code:
<td><br> <input type="submit" class="btn btn-primary btn-small" value="Search"></td>
<td><br>-or - Search Job Postings
I was attempting to do it by something like:
IE.document.Something.Click
Much appreciation for someone who can help me to figure out the "something"!!!
There are probably better ways to identify a button, but when I was testing this stuff for fun a long time ago checking the onclick property was the only reliable way to identify a button on the site I was interfacing with. I'm assuming you know how to reference the browser window based off what you said. I included the Microsoft HTML Object Library to get the HTMLButtonElement type.
Dim htmlElements as Variant
Dim buttonElem As HTMLButtonElement
Set htmlElements = IE.Document.getElementsByTagName("BUTTON")
For Each buttonElem In htmlElements
If InStr(1, buttonElem.onclick, "orbisApp.buildForm({action:'displayAdvancedSearch'}).submit()") > 0 Then
buttonElem.Click
End If
Next
All you need to do is simply use the form element to click the button. Your statement:
IE.document.Something.Click
Is almost spot on.
Here's the code you want to use
IE.document.Forms(0).Submit

Uploading Files Not Working - trim the initial characters

I am trying to upload a file using the WebBrowser control. It trims the starting character sometimes one and sometimes three then choose window gives error Invalid file name!. Can't seem to do it and need some help.
Here is the Html:
<input name="UploadedFile" id="UploadedFile" type="file" />
<input name="up" id="up" type="button" value="Upload" />
Here is the vb code:
Dim el = elc.GetElementsByName("UploadedFile")
el.Item("UploadedFile").Focus()
' SendKeys.Send("Capture.png" & "{ENTER}")
SendKeys.Send("C:\Capture.png" + "{ENTER}")
el.Item("UploadedFile").InvokeMember("Click")
that the file upload button comes up and hit enter, but can't input full filename into the file name area.
If I use thisSendKeys.Send("C:\Capture.png" + "{ENTER}"). It gives this error:
Choose window error screenshot
If I use this SendKeys.Send("Capture.png" + "{ENTER}"). It gives this error:
Choose window error screenshot
And if I put extra character then it works fine but it doesn't always trim one character so I can't put an extra character to solve this error.
The problem might be that the sendkeys comes up too fast behind focus, so the first few characters don't get picked up. Try just filling the textbox all at once with one line by setting the textbox's value rather than trying to imitate user keystrokes, this could replace both the focus and sendkeys lines:
WebBrowser1.Document.GetElementById("UploadedFile").SetAttribute("value", "C:\Capture.png")
...and then call the button-click
You are right #soohoonigan the sendkeys comes up too fast but that is not an answer for that. I did this like that.
Here is my code:
Dim el = elc.GetElementsByName("UploadedFile")
SetFile()
el.Item("UploadedFile").InvokeMember("Click")
Public Async Sub SetFile()
Await Task.Delay(1000)
SendKeys.Send("c:\Capture.png" & "{ENTER}")
End Sub
It's working fine.

how to copy text from web browser textbox/input to textbox vb.net

I have tried:
TextBox1.Text = WebBrowser1.Document.GetElementById("User1").InnerText
Info:
Textbox1 is where i wish for the text to be,
User1 is the input/textbox id
The code for site:
<input type="text" name="User" id="Pass1" value="Uwotm8">
Also i've heard of methods like stream writer grabbing but i don't know anything about that yet
probably something like this
TextBox1.Text = WebBrowser1.Document.GetElementById("Pass1").GetAttribute("value")

write text inside div element use webbrowser vb.net

Need to write in element .
This is the HTML code
< div id="t" class="message-tools__textarea js-scroller" contenteditable="true" data-placeholder="Write a message (Enter to send)">XXXXXX< /div>
My question is how to write text like XXXXXX ?????
Add this:
<div id="t" runat="server" ...
And then in your codebehind:
t.InnerText = "XXXXXXX" ' or t.InnerHTML if you're adding HTML code).
So, if the text to be replaced is fixed and has only 1 instance within the HTML code, it should be rather simple:
Dim OrigCode, ModifiedCode as string
OrigCode = GetGoogleCodeFromURL ' get the code
ModifiedCode = OrigCode.Replace("XXXXXXX","ZZZZZZ")
Dim MyHTML as string = "<head>...</head><body><h1>Hello World!</h1><p> </p>" & _
ModifiedCode & "<p> </p><p>That's it.</p>"
But usually, things are a bit more complicated, so I'm not sure, if you expressed your wish precisely. Also, if the code is big and action is recursive, it might be better to break it into parts and handle only relevant part of it, due to performance issues.

IFRAME with HTA prompt with VB

I have an HTA prompt with VB code. I would like a URL loaded within the HTA window when the "SUBMIT" button is clicked. However, I cannot find any information that is helpful for my situation. Can someone please help me include an iframe in my HTA prompt so that a website can be displayed? I can use https://www.google.com as an example. Let me know if you have any questions.
Here is the code I currently have for the submit button:
bodystring = bodystring & "<BR><BR><BUTTON CLASS='Bttn_Back' OnClick='PrevStage()'>BACK</BUTTON> <BUTTON CLASS='Bttn_Submit' OnClick='NextStage()'>SUBMIT SURVEY</BUTTON>"
Here is my section of code for the "NextStage()" function:
ElseIf STAGE = 62 Then
SaveResults()
Window.Close()
End If
Put this to the HTML of the HTA:
<iframe id="htmlhere" src="" style="display: none;"></iframe>
Then in nextStage():
document.getElementById("htmlhere").src = "http:/..."; // Loads a new document to iframe
document.getElementById("htmlhere").style.display = ""; // Setting style.display empty shows the iframe
Sorry for using JS, but my VB skills are more or less zero.