“Click” a javascript button with VBA - 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

Related

Click String That Runs Javascript

I am trying to click a string of text within a web browser control that runs some javascript, I am unsure how to click the text as the method I use for buttons does not work in this instance
Function Clk(WebBrowserID, ElementID)
WebBrowserID.Document.GetElementById(ElementID).InvokeMember("click")
Return "Clicked"
End Function
This is all that is visible to the client in regards to the clickable text
<div class="task-skip pure-g">
<div class="pure-u-1">
Next
</div>
</div>
I am completely lost on how to either click the text so that the javascript script runs or to invoke the script through code so the button does not need to be pressed, any help would be greatly appreciated.
The solution that I found and worked is as below hope this will help anyone else with the issue.
For Each Element As HtmlElement In Browser.Document.All
If Element.InnerText = "TEXT HERE" Then
Element.InvokeMember("click")
End If
Next
Return "RETURN VALUE"

Simulate Click event on a DIV element

I cannot simulate a click event on a div element. The HTML code is this one:
<div id="export" data-export="true" data-timespan="">
<button class="btn btn-default ladda-button" type="button" data-style="expand-right" data-spinner-color="#8899a6">
<span class="Icon Icon--featherDownload"></span>
<span class="ladda-label">
Exportar datos
</span>
<span class="ladda-spinner"></span></button>
<div class="hidden error server Callout Callout--danger">
<p>Ocurrió un problema al exportar tus datos, inténtalo de nuevo más tarde.</p>
</div>
</div>
In the webpage, is just a button that downloads a file when clicked. There is no URL. But after checking the code, is the div element what triggers the click event. Look the image.
Click to see image
That "g.handle" triggers a js.file form the website.
My vba code works perfect for doing other stuff in the website, but I cannot accomplish this task.
I've tried all this options:
Set div = HTMLDoc.getElementById("export")
div.FireEvent ("onclick")
div.Click
HTMLDoc.all.Item
Call HTMLDoc.parentWindow.execScript("g.handle", "JavaScript")
Nothing works. I'm kind of blocked right now and I have no idea of how to proceed. Maybe this has no solution? I have almost no idea of HTML and I have 0 idea of Javascript. But it must have a solution, because if I do it manually, i click on that DIV, wait some seconds and download my file.
Thanks in advance. Any help is welcome.
You didn't specify if the highlighted portion was the html code for the button or not, but it appears from looking at it that it isn't.
You can grab the class name for your button. The name may or may not be unique, and it's difficult to know that without seeing the entire html code, but you can set the btn variable below to the first instance of that class name. If there are multiple class names in your html code, then you will need to use a For Each...Next statement to iterate through them (unless you know the instance number off hand).
So we will go with the first instance of your class, hence the reason for the (0) appended on the end.
Sub Test()
Dim ie As New InternetExplorer, HTMLDoc As HTMLDocument, btn As Object
ie.navigate Url 'whatever this may be
' Some code to wait for your page to fully load
Set HTMLDoc = ie.document
Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
btn.Click
End Sub

Trying to fill web form with VBA, can´t address element

I am trying to fill out a web form in IE with data from a workbook but I am having trouble addressing the text box since it seems to lack a name.
The site is behind a login so no link, sorry. But the element presents itself as
<input type="text" placeholder=" Søg på brugernavn, referencenummer, eller e-mail" ng-model="filterSearch.search" ng-change="filterOnSearch()" ng-model-options="{ debounce: 500 }" class="ng-pristine ng-valid ng-empty ng-touched">
when inspecting it in chrome.
I have tried
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").Value = "11"
but VBA throws me a run-time error '438': Object doesn't support this property or method.
Any suggestions?
It may sound confusing but anytime you are trying to pull an element by classname you need to use the
.item(0)
after that you would use something similar to
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").item(0).Value = "11"
This is assuming that you are trying to fill out an input box that has the class above and that the element is the only 1 in its class or the first one.
In the event that it does not work it may not be the first element in that class and then you may need to do item(1) or item(2) etc.

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

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.

IE automation send the HTTP Post directly to the form action using VBA

I am having some automation trouble.
I am using HTML Object Library to automate an download. At the end of the automation process, I have to click a submit button of a form and then the download starts.
When I try to handle the dialog with the winapi32 function like this:
hWndDialog = 0
'Wait until the dialog is open
While hWndDialog = 0
hWndDialog = FindWindow(vbNullString, "Dateidownload")
Wend
'get the handle of the button
hWndDialogSpeichern = FindWindowEx(hWndDialog, 0, "Button", "&Speichern")
Call SetForegroundWindow(hWndDialog)
'send message
lRetval = SendMessage(hWndDialogSpeichern, &H5, ByVal 0&, ByVal 0&)
Nothing happens. I read something, that this isn't possible, because the dialog is modal?
So I try to send the POST data directly to the form's action. I think this is the best possibility even.
But I don't know what to send to the server.
Here the form in the html page:
<form action="/smarti/bismt/bismt/resexport" method="post">
<input class="active" type="button" onclick="submitform()" name="button_export" value="Export">
submitform() only check some values and then calls:
document.forms[0].submit();
However, when I send a POST request to "/smarti/bismt/bismt/resexport" I only get the page. I don't know how to set up the request header. Tried to use firebug, to see what is sending to the form, but saw nothing I recognized.
Did you try adding a question mark ? and then the names of the text input boxes? Like http://zip4.usps.com/zip4/zcl_3_results.jsp?zip5=92101.