Export button cannot be clicked on HTML page with VBA - html

I'm automating an HR webpage (where you have to login to see info, so I can't share access), however, after getting the query I want, I can't click a specific button. Code for that button is:
<div class="fbutton">
<div>
<span class="export_xls" style="padding-left: 20px;">Exportar XLS</span>
Class Name is "export_xls"
OutterText, InnerText and InnerHTML is the same: "Exportar XLS"
I have tried many things, like going into loop on every "div" or "span" object and match one of the elements I mentioned earlier and click it if found, no one is even recognizing the "Exportar XLS" text.
My code is the following:
Sub extraer_horarios()
Dim MyHTML_Element As IHTMLElement
Dim URL As String
Dim IE As Object
Dim Element As HTMLLinkElement
Dim IeDoc As Object, post As Object, elem As Object
Set IE = CreateObject("InternetExplorer.Application")
URL = "http://201.122.64.119:40/accounts/login/?next=/data/worktable/"
IE.Visible = True
IE.navigate URL
WaitFor IE
IE.Document.all.Item("id_username").innerText = "*****"
IE.Document.all.Item("id_password").innerText = "*****"
IE.Document.all.Item("id_login").Click
Application.Wait (Now + TimeValue("00:00:05"))
Set IE = GetIE("http://201.122.64.119:40/data/worktable/")
IE.navigate "http://201.122.64.119:40/page/att/AttReport/"
Application.Wait (Now + TimeValue("00:00:05"))
IE.Document.all.Item("LEReport").Click
IE.Document.all.Item("id_cometime").Value = "2019-08-01"
IE.Document.all.Item("id_endtime").Value = "2019-08-31"
IE.Document.all.Item("id_drop_emp").Click
IE.Document.GetElementbyID("id_per_count").Focus
AppActivate "BioTime"
Application.SendKeys "{END}"
Application.Wait (Now + TimeValue("00:00:05"))
Dim cBox As Object
Set cBox = IE.Document.getElementsByClassName("chk_selected_all")(0)
cBox.Click
IE.Document.all.Item("id_close").Click
IE.Document.all.Item("id_query").Click 'Until here everrything works fine
'Here the problem starts
Set HTMLDoc = IE.Document
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("*")
If MyHTML_Element.innerText = "Exportar XLS" Then MyHTML_Element.Click: Exit For
Next
End Sub
If you want any other part of the code or something I can facilitate other than the impossiblity of sharing the access to webpage, please ask me so. Thank you for your help.

Related

Open a webpage with VBA and click on the "active directory" button to log in

I want to open a web page directly from Excel VBA where I need to click on the "active directory" button which has no ID. This button uses the Windows credentials to automatically log so I just need to click there to later once I'm logged I can start populating some fields.
This generic code is giving me an error message
" The object invoked has disconnected from its clients"
I assume it's related to the web page secure log on.
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub
IE is EOL and shouldn't be used anymore.
I can't tell you how exactly on the page the button is clicked, because that requires the HTML code. But the disconnection from the client may be due to the way IE is initialized. Try it via the GUID D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E
Sub test()
Const sSiteName = "https://www.padb.ford.com/PadbStrutsWeb/treeHomePre.do"
Dim oIE As Object
Dim oHDoc As HTMLDocument
Set oIE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
With oIE
.Visible = True
.navigate sSiteName
End With
While oIE.readyState <> 4
DoEvents
Wend
Set oHDoc = oIE.document
With oHDoc
'Here I want to click on that button so I can latter populate some of the fields, once page is loaded'
End With
End Sub

Excel VBA code to click web button

Need help how to create excel vba code for this
I'll be needing the codes so I can complete my macro.
Thanks in advance
First, you will need to create a reference to:
Microsoft Internet Controls
Microsoft HTML Object Library
In VBE, click Tools > References
Sub clickLink()
Dim ie As New InternetExplorer, Url$, doc As HTMLDocument
Url = "http://UrlToYourLink.com"
With ie
.navigate Url
Do While .Busy Or .readyState < READYSTATE_COMPLETE
DoEvents
Loop
doc = .document
.Visible = True
End With
Dim myBtn As Object
Set myBtn = doc.getElementsByClassName("button rounded")(0)
myBtn.Click
End Sub
The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample#vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
The program requires references to the following:
1 Microsoft Internet Controls
2. Microsoft HTML Object Library
Microsoft internet controls are a great way to do this, but if you aren't allowed to add new references, here is another way to go about web scraping.
This methode ain't as 'clean' as Microsoft internet controls and HTML object but it gets the job done.
Sub GoogleSearch()
Dim ie As Object
Dim objSearchBnt As Object
Dim objCollection As Object
Dim i As Integer
'initialize counter
i = 0
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
'navigate to the url
ie.navigate "Www.google.com"
'Statusbar shows in the buttom corner of excel
Application.StatusBar = "Loading, please wait..."
'Wait until page is ready
Do While ie.busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Store all the elements with input tag
Set objCollection = ie.Document.getElementsByTagName("input")
'Go through all input elements
While i < objCollection.Length
'input search field
If objCollection(i).Name = "q" Then
objCollection(i).Value = "Hello World"
End If
'search button
If objCollection(i).Type = "submit" Then
Set objSearchBnt = objCollection(i)
End If
i = i + 1
Wend
objSearchBnt.Click
'Clean up
Set objSearchBnt = Nothing
Set objCollection = Nothing
Set ie = Nothing
'Give excel control over the status bar agian
Application.StatusBar = ""
End Sub

Pull value from website (HTML div class) using Excel VBA

I'm trying to automate going to a website and pulling the ratings from several apps.
I've figured out how to navigate and login to the page.
How do I pull the element - the number "3.3" in this case - from this specific section into Excel.
Being unfamiliar with HTML in VBA, I got this far following tutorials/other questions.
Rating on website and the code behind it
Sub PullRating()
Dim HTMLDoc As HTMLDocument
Dim ie As InternetExplorer
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.appannie.com/account/login/xxxxxxxxxx"
Set ie = New InternetExplorer
ie.Silent = True
ie.navigate sURL
ie.Visible = True
Do
'Wait until the Browser is loaded
Loop Until ie.readyState = READYSTATE_COMPLETE
Set HTMLDoc = ie.Document
HTMLDoc.all.Email.Value = "xxxxxxxxx#xxx.com"
HTMLDoc.all.Password.Value = "xxxxx"
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Dim rating As Variant
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
Range("A1").Value = rating
'ie.Refresh 'Refresh if required
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
The code below will let you extract text from first element with class name "rating-number ng-binding" in HTML document. By the way GetElementsByClassName is supported since IE 9.0. I use coding compatible also with older versions in my example.
Dim htmlEle1 as IHTMLElement
For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
If htmlEle1.className = "rating-number ng-binding" then
Range("A1").Value = htmlEle1.InnerText
Exit For
End if
Next htmlEle1
While Ryszards code should do the trick if you want to use the code you have already written then here is the alterations I believe you need to make.
For Each oHTML_Element In HTMLDoc.getElementById("login-form")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml

VBA Scraping with IE11 - HTML Elements not indexing in VBA Local - Call javascript instead?

I've been scraping away for the last couple months with no issues, but now I have a site I'd like to access that is a bit more complicated than simple HTML. When I inspect elements, I can find the tag, ID, and name of an input box i'd like to fill, but when I run the code, there are no elements within the VBA locals window for my ie object.
I believe this is due to AJAX, but i'm too novice with ajax/javascript to really know for sure.
This is a screenshot of the input box i'd like to fill out:
and this is the code I'm using:
Sub Findie()
Dim ie As Object
Dim userName As Variant
Dim passWord As Variant
Dim strHTML As String
Dim sht As Worksheet
Dim itemRng As Range
Dim testObj As Object
Set sht = Sheets("Sheet1")
Set itemRng = sht.Range("A2")
userName = "***#***.com"
passWord = "********"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
strHTML = "http://[MYWEBSITE].com"
ie.Navigate strHTML
'wait for browser
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:03 AM#)
ie.Document.getElementsByName("username")(0).Value = userName
ie.Document.getElementsByName("password")(0).Value = passWord
ie.Document.getElementsByClassName("submit")(0).Click
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Application.Wait (Now + #12:00:01 AM#)
ie.Navigate "[MYWEBSITE]" 'after logging in, navigating to this page saves time
While ie.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
ie.Document.getElementsByName("productId")(0).Value = "12345"
This is not an issue with the page loading either, it does not work when I step through.
I'm thinking it will be much easier if I can actually run the scripts that are on the server, but I've never done that using VBA. I'm not really sure how. Here is a screenshot of the scripts available on the server. I don't really know where to go from here, or which method is ideal.

Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated

I'm trying to use excel to automate the value entering in a time sheet. The time sheet is on a web page.
Right now I'm able to load the page, enter my username and password and then entering the time sheet by itself. See code below.
At this point I need to click on a button to open sub-forms. I can't know in advance how many sub-forms there will be to open. I know how to click on a button when it has a "name". But in this case there's none. So my updated code below use a loop to open every other subform. It works the first time, but when I do it again
Could someone point me how to determine how many of those button there is in the page and how to click on each?
Following I'm placing the code I have until now and below it, the HTML code of the page I need to interact with.
Private Sub time_sheet_filling()
Dim I As Long
Dim IE As Object
Dim doc As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "http://timesheet.cccc.ca/timesheet/"
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Load the logon page
Set objCollection = IE.Document.getElementsByTagName("input")
I = 0
While I < objCollection.Length
If objCollection(I).Name = "txtUserName" Then
' Set text to enter
objCollection(I).Value = "6666"
End If
If objCollection(I).Name = "txtPwd" Then
' Set text for password
objCollection(I).Value = "password"
End If
If objCollection(I).Type = "submit" And objCollection(I).Name = "btnSubmit" Then ' submit button clicking
Set objElement = objCollection(I)
End If
I = I + 1
Wend
objElement.Click ' click button to load the form
' Wait while IE re-loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' Show IE
IE.Visible = True
Dim links, link
Dim n, j
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
For j = 0 To n - 1 Step 2
links(j).Click
'I have some operations to be done will post another question for this
IE.Document.getElementById"DetailToolbar1_lnkBtnSave").Click 'save
IE.Document.getElementById"DetailToolbar1_lnkBtnCancel").Click 'close
Next
End Sub
So extract of the html code is below. I'm trying to click the button that is coded in the last line of the html code below
<table width="984" class="Grid" id="dgTime" border="1" rules="all" cellspacing="0">
<tbody>
<tr class="GridHeader">
</tr>
<tr class="GridItem">
</tr>
<tr class="GridItem">
<td class="GridButtonColumn">
<a href="javascript:__doPostBack('dgTime$_ctl2$_ctl0','')">
<img src="images/toolbar/b_edit.gif">
</a>
</td
Tx Tim for the answers. Now I'm able to select the first subform button to open it.
links(j).click 'j = 0
I then save it, close, and come back to the main form. But then when I try to do
links(j).click 'j = 2 this time
the second time I get a runtime error 70: permission denied. Anymore kind help will be so appreciated.
Regards
With the kind help from Tim Williams, I finally figured out the last détails that were missing. Here's the final code below.
Private Sub Open_multiple_sub_pages_from_main_page()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim valeur_heure As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "http://webpage.com/"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
Set objCollection = IE.Document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "txtUserName" Then
' Set text for search
objCollection(i).Value = "1234"
End If
If objCollection(i).Name = "txtPwd" Then
' Set text for search
objCollection(i).Value = "password"
End If
If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click ' click button to load page
' Wait while IE re-loading...
While IE.Busy
DoEvents
Wend
' Show IE
IE.Visible = True
Set Doc = IE.Document
Dim links, link
Dim j As Integer 'variable to count items
j = 0
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
While j <= n 'loop to go thru all "a" item so it loads next page
links(j).Click
While IE.Busy
DoEvents
Wend
'-------------Do stuff here: copy field value and paste in excel sheet. Will post another question for this------------------------
IE.Document.getElementById("DetailToolbar1_lnkBtnSave").Click 'save
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now) 'wait
Loop
IE.Document.getElementById("DetailToolbar1_lnkBtnCancel").Click 'close
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now) 'wait
Loop
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
j = j + 2
Wend
End Sub
IE.Document.getElementById("dgTime").getElementsByTagName("a")(0).Click
EDIT: to loop through the collection (items should appear in the same order as they are in the source document)
Dim links, link
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
'For Each loop
For Each link in links
link.Click
Next link
'For Next loop
Dim n, i
n = links.length
For i = 0 to n-1 Step 2
links(i).click
Next I
CSS selector:
Use a CSS selector of img[src='images/toolbar/b_edit.gif']
This says select element(s) with img tag with attribute src having value of 'images/toolbar/b_edit.gif'
CSS query:
VBA:
You can apply the selector with the .querySelector method of document.
IE.document.querySelector("img[src='images/toolbar/b_edit.gif']").Click