How to click this button? - vba

Hi all I am a beginner in Visual Basic. Your help is highly appreciated.
How can i click this button in a webpage?
<a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
<span>Get Started</span>
</a>

Short example which worked for me, tested with simple html file:
ClickA.html:
<!DOCTYPE HTML>
<html lang="en">
<head>
<title><!-- Insert your title here --></title>
</head>
<body>
<a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
<span>Get Started</span>
</a>
</body>
</html>
vba standard module:
' Add References:
' - Microsoft HTML Object Library
' - Microsoft Internet Controls
Sub test()
Dim Browser As InternetExplorer
Dim Document As htmlDocument
Set Browser = New InternetExplorer
Browser.Visible = True
Browser.navigate "C:\Temp\VBA\ClickA.html"
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Dim anchorElement As HTMLAnchorElement
Set anchorElement = Document.getElementsByClassName("buttonRed").Item(Index:=1)
anchorElement.Click
Set Document = Nothing
Set Browser = Nothing
End Sub

actually this is not a button. its anchor tag. write your code as
<a class="buttonRed" href="what_would_you_do.html">
<span>Get Started</span>
</a>
and it will redirect to "what_would_you_do.html page (if its in root)

Related

How to Target Specific Element in Internet Explorer using Access VBA

First off, please forgive me for any mistakes in posting - this is my first time posting (although I'm a frequent visitor!
I'm working on a program to improve productivity within my workgroup. I am attempting to link an Access database to an internal web application (Pega based) that is very complex. Unfortunately, my knowledge of coding is very basic, and I do not have the available funding or bandwidth to obtain formal assistance. While I would love to use add-ins, I cannot, since my company will not allow it (I've asked, begged, pleaded, etc).
This is what the page of the program looks like. I first need to click the Work Manager Tab (WMT) blocked in GREEN, then I need to click the Get Next button (NGN) blocked in RED.
Steps I think need to happen
Bind the correct IE window session
Click on WMT
Click on NGN
I have been able use Access to bind the correct window and tab of IE (thank you to the person who provided the basic code for this!) but have not yet been able to decipher how to target the correct element on the page. The naming structure is a bit too much for my knowledge.
This is a screenshot of the HTML for the WMT (it won't copy it, at least not legibly, even after cleanup). I would love to be able to use the ID for the specific element, however, it's dynamic, and changes with each interaction, which is why I've been trying to target the static pieces, highlighted in green.
I haven't done much work with the second piece, however, This is the screenshot for that, with the button highlighted in yellow, if anyone wants to help there, too.
Thank you!
CODE FOR THE WINDOW BIND (works all the time so far!)
Sub C360WindowFind()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "Coverage User" & "*" Then
Set C360Window = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("C360 window is not found. Please ensure C360 is open in Internet Explorer and try again")
Else
'DO THE OTHER THINGS
End If
End Sub
CODE I'VE TRIED TO TARGET CORRECT TAB (Add in place of "DO THE OTHER THINGS" in previous code set)
FIRST TRY (does some crazy stuff that ends up making the program log out)
FindWorkManager1()
Dim C360doc As HTMLDocument
Dim Element
Dim ULTabName As String
Dim LIElement As String
Dim WMT_HREF As String
ULTabName = "yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList"
ULElement = "getElementsByClassName"
LIElement = "getElementbyID"
WMT_HREF = "getElementsByTagName"
Set C360doc = C360Window.document
With C360doc.all
If element = ULElement("ULTabName").LIElement("Work Manager").innerText.WMT_HREF("a")(0) Then
element.Click
Else
MsgBox ("Please ensure Work Manager is open, then try again")
End If
End With
End Sub
SECOND TRY: Gets to the lines with stars and quits (tried a couple variations of this with same results)
FindWorkManager2()
Dim C360doc As HTMLDocument
Set C360doc = C360Window.document
TabTagCounter = 0
TabItemCounter = 0
With C360doc.all
Set Header = C360doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")
'Find the correct TabTag within Header
For Each TabTag In Header
If TabTag.tagName Like "UL" Then
TabTagCounter = 1
'If we've identified the correct TabTag, then look at the TabItem
'*************Not currently working beyond this point :(
If TabTagCounter = 1 Then
For Each TabItem In TabTag
If TabItem.innerText Like "*Work Manager*" Then
TabItemCounter = 1
End If
MsgBox ("try again")
Next
Else
End If
End If
Next
End With
End Sub
I think you should try to click the <a> tag as it's a link navigating to #Tab2. If the class yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList is the only one, you could locate ul with this class name first, then locate the li and a with tag name.
As the real link is not accessible, so I make a test page like below:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="tStrCntr">
<ul class="yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList">
<li tabindex="-1"title="Work Manager"></li>
<li tabindex="0" title="Home" id="Tab2">
<a tabindex="-1" id="TABANCHOR" href="https://www.google.com">
<span style="display: inline-block">
<label>Work Manager</label>
</span>
</a>
</li>
</ul>
</div>
</body>
</html>
And the VB code to click Work Manager in the test page is like below, you could have a test:
Updated version:
Sub LOADIE()
Dim link As Object
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://the test web page"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set ElementCol = doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")(0).getElementsByTagName("li")
For Each link In ElementCol
If link.getAttribute("title") = "Work Manager" Then
link.getElementsByTagName("a").Item(0).Click
End If
Next link
End Sub

VBA - How to get elements by src or alt at internet explorer

I'm having a little trouble with my code.
Follow the code below
<img src="IMAGE LINK HERE" border="0" alt="Page suivante" align="right">
For reasons of confidentiality, I canno't show the link. I would like to know, how can I click on this link using "alt".
Thanks
I'm not too familiar with CSS selectors (so am sure someone else can/will give you a better answer), but this seems to work for me if I add a reference for Microsoft HTML Object Library:
Option Explicit
Private Sub SomeCssSelection()
Dim html As MSHTML.HTMLDocument
Set html = New MSHTML.HTMLDocument
html.body.innerHTML = "<img src=""IMAGE LINK HERE"" border=""0"" alt=""Page suivante"" align=""right"">"
Dim targetElement As MSHTML.HTMLImg
Set targetElement = html.querySelector("img[alt=""Page suivante""]")
Debug.Assert Not (targetElement Is Nothing)
End Sub
As pointed out by QHarr, single quotes can be used to make the code easier to read:
Set targetElement = html.querySelector("[alt='Page suivante']")

Html agility pack failed to scrape image

Ok I have found a code which as the website declared scrape image from div using htmlagility pack vb.net.
I followed the procedure and I get nothing.
This is source html:
<div class='my-gallery'>
<!-- ONLY PREV NAVIGATION -->
<!-- ONLY PREV NAVIGATION -->
<img src='http://example.com/image.jpg' alt='image'/>
<!-- ONLY NEXT NAVIGATION -->
<!-- ONLY NEXT NAVIGATION -->
</div>
This is vb.net code I tried:
Public Sub getImg()
Try
Dim link As String = ("http://www.exmple.com")
'download page from the link into an HtmlDocument'
Dim doc As HtmlDocument = New HtmlWeb().Load(link)
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[#class='my-gallery']//img//src")
If Not div Is Nothing Then
PreviewBox.ImageLocation = (div.ToString)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The src is an attribute of the img element, so you need to extract it slightly differently, for example:
Dim img As HtmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[#class='my-gallery']//img")
If img IsNot Nothing Then
Dim url As String = img.Attributes("src").Value
PreviewBox.ImageLocation = url
End If

VbScript hta - Open a new hta by link and retrieve correct filename

I have two files; "1.hta" and "2.hta".
"1.hta" contains a simple link to file "2.hta"
2.hta
"2.hta" contains a script to determine its own filename
FullName = replace(oApp.commandLine,chr(34),"") 'oApp = HT Application ID
arrFN=split(FullName,"\")
FileName = arrFN(ubound(arrFN))
SourceDir=replace(FullName,FileName,"")
"2.hta" works perfectly when started "stand-alone" --> FileName = 2.hta
However, starting "2.hta" via link from "1.hta" I get --> FileName = 1.hta
I need a way to determine the correct filename, or does hta always retrieve the filename of the first/starting instance?
You can try like this :
<html>
<head>
<title>HTA Launch another HTA</title>
<HTA:APPLICATION
SINGLEINSTANCE="yes"
WINDOWSTATE="maximize"
>
</head>
<SCRIPT Language="vbscript">
Sub Execute(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run chr(34) & File & chr(34)
End sub
</SCRIPT>
<body>
<h1>This is test hta 1 ONE</h1>
Start the HTA2
</body>
</html>

VB.NET Webbrowser How to display specified content I want

eg: the source code will be:
<html>
<head> balaba....
</head>
<body>
<div id="many_div">...</div>
<div id="main">
<div id="target">
.....balabala ...
</div>
</div>
</body></html>
Then, how to let my webbrowser only display the div with "target" id ?
Thanks!
You'll need to manipulate the HTML of the page.
I would use HtmlAgilityPack to extract the part you want and rewrite it to the same or another file:
Dim html = File.ReadAllText("c:\temp\htmlTest.htm")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim target = doc.GetElementbyId("target")
If target IsNot Nothing Then
Dim body = doc.DocumentNode.SelectSingleNode("//body")
body.RemoveAll()
body.PrependChild(target)
Using writer = File.OpenWrite("c:\temp\htmlTest2.htm")
doc.Save(writer)
End Using
End If
Now you just have to load this html in the WebBrowser.
If you want to get the HTML directly from internet/intranet:
Dim client As New HtmlAgilityPack.HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = client.Load("http://yoururl.com")
' rest is the same as above(without doc.LoadHtml) '