VB.net click site button - vb.net

I have been trying to 'click' a button on a site , but I can't get it working.
This is the button I'm talking about:
<div data-expected-currency="1" data-asset-type="T-Shirt" class="PurchaseButton btn-medium btn-primary" data-se="item-buyforfree" data-item-name="Yoshi tux" data-item-id="1788861" data-expected-price="0" data-product-id="231582" data-expected-seller-id="78049" data-bc-requirement="0" data-seller-name="robosapien626">
Take One
</div>
This is what I'm trying:
For Each elem As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If elem.InnerText.Trim = "PurchaseButton btn-medium btn-primary" Then
elem.InvokeMember("click")
End If
Next
It does not do anything.
Other buttons with an actual ID do work fine.

You have to use GetAttribute after the tag name so you can find the button and click it:
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If element.GetAttribute("class") = "PurchaseButton btn-medium btn-primary" Then
element.InvokeMember("click")
End If
Next

The .InnerText of that element is Take One with some whitespace around it, so you can try
If elem.InnerText.Trim = "Take One" Then
or
If elem.OuterHtml.Contains("PurchaseButton btn-medium btn-primary") Then

Related

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

Use VBA to click on a button in IE

I'm currently looking for a way to click on a button on a web page to automate an export.
I've already succeeded with logging in to the web page and clicking on the login button to redirect the page, but then I could not click on the next button i'm looking for.
My code :
For Each ele In IE.document.getElementsByTagName("ul")
If ele.document.getElementsByTagName("a").getAttribute("aria-labelledby") = "Exporter vers CSV" Then ele.Click
Next
Website source:
<ul class="dropdown-menu pull-left">
<!-- ngRepeat: item in secondaryItems track by item.dataAid --><li ng-repeat="item in secondaryItems track by item.dataAid" class="">
<a role="button" ng-show="item.visible" aria-labelledby="Exporter vers CSV" data-aid="tool-bar-inner-dd-btn-export" type="submit" ng-disabled="!item.enabled" ng-class="{plToolbarItemDisabled:!item.enabled, disabled:!item.enabled}" class="grid-export-item-btn" ng-click="item.callback(item, $event)" pl-toolbar-button-in-dropdown="" item="item">
I cannot test it without knowing your URL, but this maywork:
For Each ele In IE.document.getElementsByTagName("ul")
For Each ele2 In ele.getElementsByTagName("a")
If InStr(ele2.InnerHtml, "aria-labelledby=""Exporter vers CSV""") Then ele2.Click
Next ele2
Next ele
Give this a go. When you are upon instr() function then simply the below method should get you there.
For Each ele In IE.document.getElementsByTagName("a")
If InStr(ele.getAttribute("aria-labelledby"), "Exporter vers CSV") > 0 Then ele.Click: Exit For
Next ele

VB.NET webbrowser click button

I know this question has been asked many times and I have tried quite a few and none work.
I have a WebBrowser and 1 button. When I click the form button it needs to invoke a click on the WebBrowser button.
I have tried
WebBrowser1.Document.All("start-button").InvokeMember("click")
WebBrowser1.Document.GetElementById("convert1").InvokeMember("Click")
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
If element.GetAttribute("id") = "convert1" Then
element.InvokeMember("click")
End If
Next
None of the above works.
Link to website
Code from the webpage source
<a class="start-button" href="javascript:;" id="convert1" onmouseover="changeImage(this, '');" onmouseout="changeImage(this, '');">
Start
<i class="fa fa-angle-right fa-5"></i>
</a>
Also the button on the WebBrowser, when I click it nothing happens for some reason.
Any help would be great.
Thanks

Clicking HTML button in vb.net

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") = "Next" 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?
I was having the same problem as you. The InvokeMember looks correct so I can only assume its not picking up the correct html element. Try refering to the element explicitly using the value. The following code worked for me but I have adapted it for you:
For Each webpageelement As HtmlElement In allButtons
If webpageelement.GetAttribute("value").Equals("E712050-15") Then
webpageelement.InvokeMember("click")
Exit For
End If
Next
Good Luck :)

How to select specific button by value instead of ID in webbrowser control?

I'm trying to make an Aduto-Forum poster with VB.NET windows form application
This is the page, http://www.inviteshare.com/community/viewforum.php?id=9
First I'm trying to log-in automatically
I insert username & pass but I can't click the Login button because there are 2 buttons with the same ID on the page.
(
input type="image" name="submit" class="submit" value="submit" />
input type="image" name="submit" value="login" />)
How can I click the second button. I need to select the button by its value I suppose;
Dim txtUser As HtmlElement = wb.Document.GetElementById("login_user")
Dim btn As HtmlElement = wb.Document.All("submit")
txtUser.SetAttribute("value", "wolfied")
If btn.GetAttribute("value") = "login" Then
btn.InvokeMember("click")
End If
But it did not work, how can I select the button that I need?
For Each elem As HtmlElement In wb.Document.GetElementsByTagName("input")
If elem.GetAttribute("value") = "login" Then
txtUser.SetAttribute("value", "wolfied")
txtPass.SetAttribute("value", "xxxx")
elem.InvokeMember("click")
End If
Next
I found the button by searching each element by its tag, then look for its value