"Clicking" anchor element to navigate to next page VBA - html

I'm creating an automation that will go through almost 110 pages with VBA. These pages have identical layout. I would need to go from one page to another automatically by "clicking" next button. At the very end of every page, there is a "button" (list anchor) that says "Next page". Problem is that the source code does not contain ID which would make it easy to refer with:
getElementById("id").Click
I open browser. That works fine. and I've tried something like this but it doesn't work:
Dim ieDoc As Object
Dim links As Object
Dim link As Object
Set ieDoc = ieApp.Document
Set links = ieDoc.Anchors
For Each link In links
If link.innerHTML = "innerHTML" Then
link.Click
Exit For
End If
Next link
I have tried almost everything I could find from stackoverflow but nothing worked for my needs.
THis is the source code of the "Next button" that I'm trying to click:
<li class="pager-next"><a title="Next page" href="/fi/tyosuhde- edut/kayttokohdehakupage=1&service_type=lunch&keywords=&city=&service=&service_areas=&payment_method=&municipality=&service_coupon_code=&items_per_page=50">seuraava ›</a></li>
I quess the problem is that the ClassName is in "li" and not in "a"?
Could some help me??
EDIT
Found a workaround!!:
Set pages = doc.getElementsByTagName("a")
For Each page In pages
If (page.getAttribute("title") = "Siirry seuraavalle sivulle") Then
page.Click
End If
Next page

You will need to keep reseting the html document with each refresh.
After a refresh try
ieApp.document.querySelector("a[title=""Next page""]").Click
CSS Selector
More info about CSS Selectors: CSS selectors
EDIT:
In your case the actual HTML selector is
appIE.doc.querySelector("a[title = ""Siirry seuraavalle sivulle""]").Click
Note there is no space after the "a" and you will need to leave enough time between clicks to allow the new page to load.

Related

VBA: How to click a drop down in a web page with the help of HREF and CLASS HTML tag avilable

I am trying to automate one of my website working on a secured server, the code which help me to log into the web page but after that I have to click a dropdown from a menu bar where the HTML tag available is CLASS and HREF only.
HTML
<li>
<a Class = "childmenu" href="./1713363899/ViewLogsPage">
<span>Logs</Span>
</a>
</li>
A part of my VBA code is
Dim MyHTML_Element as IHTMLElement
Set HTMLDoc = MyBrowser.document
For Each MyHTML_Element In HTMLDoc.getElementsByClassName ("childmenu")
If MyHTML_Element.href = "./1713363899/ViewLogsPage" Then MyHTML_Element.Click:
Exit For
Next
but this code not working and Skip after executing this line ".....ClassName ("childmenu")" and jump to END sub, when i run my code in Debug mode.
Try
If Instr(MyHTML_Element.href, "1713363899/ViewLogsPage") Then MyHTML_Element.Click
If not works, add debug.print MyHTML_Element.href before line with If. It is possible that link is generated while page is being download.
Use a CSS selector of .childmenu[href='./1713363899/ViewLogsPage']. Which reads as the elements with classname childmenu having attribute href with value './1713363899/ViewLogsPage'
CSS selector in action:
VBA:
You apply the CSS selector with the .querySelector method of the .document
MyBrowser.document.querySelector(".childmenu[href='./1713363899/ViewLogsPage']").Click
Info on:
CSS selectors
.querySelector method
Bonus track:
Feast on CSS selectors to home your CSS ninja skills - I have no affiliation with this. Found it today and it was fun.
Or try the tester via this: CSS Selector Reference

How do I click a button on a webpage through VBA?

I am trying to use click a button through vba but it tells me that the "Object doesn't support this property or method".
My VBA code right now is:
IE.document.getElementByType("button").getelementByClass("qm_historyTab_GoButton").Click
The HTML for the button is:
<button class="qm_historyTab_GoButton" type="submit" style="font:normal 11px tahoma,arial,helvetica,sans serif;">
<span class="qm_historyTab_GoButtonText">GO</span>
</button>
Try this:
IE.document.getElementsByTagName("button") _
.getelementsByClassName("qm_historyTab_GoButton")(0).Click
Both of the getXXX methods return a collection of items (even if there's only one match in the document) so to address a single item from the last one in order to click it you need to add the (0)
EDIT: if the aim is to submit the form then this will also work
IE.document.getElementById("qm_historyForm_7871").submit
or as there's only one button with that classname:
IE.document.getElementsByClassName("qm_historyTab_GoButton")(0).Click
Ok so I found the solution in this specific instance. The button is a submit type and sends a message using HTMLInputElement.
What I needed to do was add "Microsoft HTML Object Library" to my references and utilize those features.
I had to create the variable that would send the response:
Dim htmlInput As MSHTML.HTMLInputElement
I couldn't figure out how to refer to this specific element individually so I grabbed everything with it's class name.
For Each htmlInput In IE.document.getElementsByClassName("qm_historyTab_GoButton")
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
Then I had the program cycle through each item in the series and send a submit response. Since this was the only one with the class name, I got lucky.
So anyways, that's how I solved the problem.

Clicking this button in VB?

I need to click a specific button, but I cant figure out how...
Heres the code for the button from the webpage:
<input type="image" src="images/search2.png" onmouseover="this.src='images/search2hi.png'" onmouseout="this.src='images/search2.png'" onclick="showCarrier(document.getElementById('small').value, document.getElementById('large').value);return false"/>
How do I click this button?
I've already tried
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("image")
element.InvokeMember("click")
Next
But it clicks an ad instead. Any tips?
You are probably clicking the only first link with your code as your browser will navigate away from the page on the first click.
You could try the following (not tested):
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("image")
dim src as String = element.GetAttribute("src")
If src isnot nothing andalso sec.EndsWith("images/search2.png") Then
element.InvokeMember("click")
end if
Next
It will search the element identified by the image it displays.

VB WebBrowser click button

On this web page: https://www.youtube.com/upload_defaults
I would like to call a function and then click the "Save" button in the upper right corner.
There is no ID or Name. How can I do this?
You can use the distinct CSS class of the button: account-save-button
In VB.NET, use HtmlDocument.GetElementsByTagName like this:
Private Sub ClickSaveButton()
If (WebBrowser1.Document IsNot Nothing) Then
Dim Elems As HtmlElementCollection
Dim WebOC as WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("BUTTON")
For Each elem As HtmlElement In Elems
Dim CssClass As String = elem.GetAttribute("classname")
If ((CssClass IsNot Nothing) And (CssClass.Length <> 0)) Then
If CssClass.ToLower().Contains("account-save-button") Then
elem.InvokeMember("click");
Exit For
End If
End If
Next
End If
End Sub
To verify it with just a web browser, try this from your browser's developer console; it works because youtube uses jQuery:
$(".account-save-button").click();
Or pure javascript: document.getElementsByClassName('account-save-button')[0].click();
For reference, here is the button's markup:
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-primary account-save-button account-action-button" type="submit" onclick=";return true;"><span class="yt-uix-button-content">Save</span></button>
Other thoughts:
This is basically screen scraping, which can lead to brittle applications when the page changes, etc.
Google may already have a web service API for what you are trying to accomplish.. but that's a good question:
What exactly are you trying to accomplish? What is the context? We may be able to suggest a better alternate solution.
Update:
I checked the Youtube API Reference, but I don't see anything for setting preferences for upload defaults.

Clicking a button within an IE table

I am trying to click a button within a table on a webpage within IE, the source of the button shows:
<input type="image" src="img/testimg.png" onclick="picture_return(this,'92b84574a336a090618f151b6fc821cf:5','http://testwebpage.com/in/834');" value="Test Web Button">
This is a part of a large table with multiple <td> within the source, this is within another table which is then within the following class:
<div class="section_client_dnBox">
I tried to go through a few of the items within the class by using the following VBA code:
IE.Document.getElementsByClassName("section_client_dnBox")(0).Click
However, had no luck as (0) didn't press anything and anything larger ie, (1) gave me an error. So my question now is basically, is there any way of clicking the button using something simple such as reffering to it's value within the table (value="Test Web Button")?
From my experience, you need to look at the tag name rather than the class name. This is an example of the code I generally use when finding buttons.
For Each MyHTML_Element In document.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then
MyHTML_Element.Click: Exit For
End If
Next
You might be able to change the . type to = "image". I too am just learning how to use IE automation in VBA so I am not a champ at it either. I hope that helps.
CSS selector:
It is far simpler to use a CSS selector of input[value='Test Web Button']. No loop required.
It says get element with input tag having attribute value having value = 'Test Web Button'. "[]" means attribute.
.querySelector method of document is how you apply the selector.
CSS query:
VBA:
ie.document.querySelector("input[value='Test Web Button']").Click