Web Side Login / no debug.print shown in specific Window - html

I want to login a web side
After a lot of trial and errors I finally wrote a code that works pretty well.
.. but got probs
First I got a prob that occurs and said
no connection to server
This Prob I solved
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
The next Prob is that when I ran the code Step by step (Trigger F8) the code runs pretty well.But Triggering F5 I got the Prob in Line
Set UserNameInput = LoginForm.getElementsByClassName("prePopulatedCredentials")(0)
This.. I do not understand
The next Prob is a debug.print "Prob"
Although I activated the Window (STR+G) debug.print is not shown.
Why want I see the debug Print result?
So.. I want to find out which Button I have to trigger by using the VBA Code
In my case I have 3 options
Search, new Search, go back or Sign In
For Sign In I found a solution for another web Side
Sub TestLogin()
Dim...
Dim LoginForm As MSHTML.HTMLFormElement
.. code
LoginForm.submit
For another Web Side
I want to find the buttons
Search, new Search, go back
The code is almost the same
Sub Get()
Dim...
Dim HTML Button AS MSHTML.IHTMLELEMENT
... Code
Set Buttons= HTMLDoc.GetElementsByTagName("button")
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButton.className, HTMLButton.tagName, HTMLButton.Id, HTMLButton.innerText
Next Button
End Sub
In this case the code runs until the line
Set Buttons= HTMLDoc.GetElementsByTagName("button")
and it jumps over to End Sub
Here my Code for the first web side with the Prob Triggering the code with F8 or F5
Sub TestLogin()
Dim ie As SHDocVw.InternetExplorerMedium
Sub TestLogin()
Dim ie As SHDocVw.InternetExplorerMedium
Dim doc As MSHTML.HTMLDocument
Dim LoginForm As MSHTML.HTMLFormElement
Dim UserNameInput As MSHTML.HTMLInputElement
Dim PasswordInput As MSHTML.HTMLInputElement
Set ie = New SHDocVw.InternetExplorer
' Here I found a solution in the www regarding the Prob Net.Framework
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
ie.Visible = True
ie.Navigate Sheet1.Range("B1").Text
Do While ie.ReadyState <> READYSSTATE_COMPLETE And ie.Busy
Loop
Set doc = ie.Document
Set LoginForm = doc.getElementById(Sheet1.Range("B4").Text)
Set UserNameInput = LoginForm.getElementsByClassName("prePopulatedCredentials")(0)
Set PasswordInput = LoginForm.getElementsByClassName("prePopulatedCredentials ")(1)
UserNameInput.Value = Sheet1.Range("B2").Text
PasswordInput.Value = Sheet1.Range("B3").Text
Stop
'LoginForm.submit
End Sub
An here the code for the 2nd Web Side with the Prob debug Print
Sub Get()
Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim UserNameInput As MSHTML.HTMLInputElement
Dim VornameInput As MSHTML.HTMLInputElement
Dim HTMLButton As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim SecurityWindow As Object
Set ie = New SHDocVw.InternetExplorer
Set ie = CreateObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
ie.Visible = True
ie.Navigate Tabelle1.Range("B1").Text
Do While ie.ReadyState <> READYSSTATE_COMPLETE And ie.Busy
Loop
Set HTMLDoc = ie.Document
Set UserNameInput = HTMLDoc.getElementById("sur")
Set VornameInput = HTMLDoc.getElementById("given")
UserNameInput.Value = Sheets1.Range("B2").Text
VornameInput.Value = Sheets1.Range("B3").Text
Stop
Set HTMLButtons = HTMLDoc.getElementsByTagName("btn")
For Each HTMLButton In HTMLButtons
Debug.Print HTMLButtons.className, HTMLButtons.tagName, HTMLButtons.ID, HTMLButtons.innerText
Next HTMLButton
End Sub
What have I done wrong or missed?
Thx for any help
And.. most important
Take all of you care
Best wishes, stay safe and healthy!
Pete

For your first issue:
It looks like a timing-related issue.
I suggest you make your code wait for some seconds before executing the problematic line of code.
It can help to fetch the value and solve this issue.
For your second issue:
As I informed you in my previous comment, you can create a new thread with detailed information. We will try to check it and try to provide you further suggestions for it.

Related

VBA web scraping issue - how to navigate specific web using html structure (href / child/ )

Hello dear VBA collegues :)
Sub login()
'test
Const URL$ = "https://kwm.kromi.de/cgi-bin/kwm?HTML=frontend/login.htm"
Dim UserName As String, Password As String, LoginData As Worksheet
Set LoginData = ThisWorkbook.Worksheets("Sheet1")
UserName = LoginData.Cells(1, "B").Value
Password = LoginData.Cells(2, "B").Value
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.Navigate URL
ieBusy IE
.Visible = True
Dim oLogin As Object, oPassword As Object
Set oLogin = .document.getElementsByName("VS_LOGIN")(0)
Set oPassword = .document.getElementsByName("VS_PASSWORD")(0)
oLogin.Value = UserName
oPassword.Value = Password
.document.forms(0).submit
ieBusy IE
Stop
'.document.getElementsByTagName("a")(2).href
'.document.getElementsByClassName("link3").Click
.Navigate2 ""
ieBusy IE
Stop
End With
'''
End Sub
Sub ieBusy(IE As Object)
Do While IE.Busy Or IE.readyState < 4
DoEvents
Loop
End Sub
And the first task is work, macro log in to website. I need to go deeper and click something but structure of web is too much for my small head I am looking some examples on website but nothing work. I showed code of website below. I need to click button "statystyka".
/html/body/div[1]/div[1]/a[2] - Xpath adress
[link picture]https://ibb.co/2Pgx2tn
May you give me some help please :)
edit:
I tried use something like this:
'.document.getElementsByTagName("a")(2).href but this not good way on thinking
You need to move into the appropriate frame, add a wait as I am using .Navigate to the frame src, then you can target by a substring of the onclick attribute:
ie.navigate ie.document.querySelector("[name=Navigator]").src
ieBusy ie
ie.document.querySelector("[onclick*=statistic]").click
If You want navigate by tag You need to set frame as is below
Dim doc As HTMLDocument
Dim doc2 As HTMLDocument
Dim lnk As HTMLLinkElement
Set doc = IE.document
Set doc2 = doc.frames("Navigator").document
Set lnk = doc2.getElementsByTagName("A")(1)
lnk.Click
#QHarr #Raymond Wu :) Thank You for trying help, maybe that will be solution in others

Referencing to an iframe in vba

as my previous question doesnt fit anymore, im creating a new one.
I have the following code to open an window. in that windows i want to open the sorting "function" and then select which attribute to sort.
Page opening works
What doesnt work, is trying to address the attributes of the sorting drop down menu, so that it can select the right one. i always get debug.print length = 0 in the last code line
The code:
Sub testalt()
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ie As InternetExplorerMedium
Dim enumm As String
Dim ifrm As MSHTML.HTMLDocument
Dim selection As MSHTML.IHTMLElementCollection
enumm = "E2711846"
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate "https://plm.corp.int:10090/enovia/common/emxFullSearch.jsp?pageConfig=tvc:pageconfig//tvc/search/AutonomySearch.xml&tvcTable=true&showInitialResults=true&cancelLabel=Close&minRequiredChars=3&genericDelete=true&selection=multiple&txtTextSearch=" & [enumm] & "&targetLocation=popup"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
Set ifrm = HTMLDoc.frames(1).document
Set selection = ifrm.getElementsByTagName("select")
Debug.Print selection.Length
End Sub
This is the, i think, relevant html part of the opened page. Sry dont know how to copyit
this is the iframe, which is on the top

Using VBA to navigate a href in IE HTML document

I am trying to use VBA to navigate IE to search some of the information for me. However, after the code is written and I run it. I found everything is fine before the part of clicking href, but if I use "F8" to execute the code one by one and it works. Unfortunately, I haven't found some situation like mine in this forum. Can someone give some hints to me? Thanks
Sub GetData()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButton As MSHTML.IHTMLElement
Dim HTMLAllhref As MSHTML.IHTMLElementCollection
studentid = 12345678
Set IE = New InternetExplorerMedium
IE.Visible = True
IE.Navigate "https://xxx.xxx"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("abcd")
HTMLInput.Focus
HTMLInput.FireEvent("onchange")
HTMLInput.Value = studentid
Set HTMLButton = HTMLDoc.getElementById("submit")
HTMLButton.Click
The part above works properly, but the following part works only if I use "F8" to execute one by one
Set HTMLAllhref = HTMLDoc.GetElementsByTagName ("a")
For Each link in HTMLAllhref
If InStr(link.innerText, studentid) Then
IE.Navigate link.href
Exit For
End If
Next
End Sub
1) If works with F8 it is usually a timing issue and you need to determine where a wait needs to happen.
As a minimum be sure to use a proper wait after each .Navigate2, .Click and .Submit to allow for page loading:
While IE.Busy Or ie.readyState < 4: DoEvents: Wend
Potentially also after:
HTMLInput.FireEvent("onchange")
2) The recommended method is .navigate2, not .navigate.

Webpage search filter inputs not detected when apply button is pressed via Excel VBA

I'm trying to write a VBA program which will find the person holding a specific position at a specific company via LinkedIn. I've already figured out how to open the LinkedIn search window, open all filters, and input the desired company name and position, but once I hit the "Apply" button to apply those filters, they simply aren't recognized as filters. It clicks the button as if I never input anything into those filter boxes at all.
I've figured out that the problem is not my method of clicking the apply button, but instead the lack of input recognition. My input strings are only recognized as filters if their respective filter area/box is clicked on before or after the string is added.
With all that being said, I know that the solution I'm looking for is a way to input the string and then click into that same filter box before I click the apply filters button.
Below is my code to input my specific filters into LinkedIn and click the apply button.
Sub Fill()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim AllFiltersButton As MSHTML.IHTMLElement
Dim ApplyButton As MSHTML.IHTMLElement2
Dim ApplyButtons As MSHTML.IHTMLElementCollection2
Dim Company As String, Position As String
Company = Range("A2").Value
Position = ("""" + Range("B1").Value + """")
IE.Visible = True
IE.navigate "https://www.linkedin.com/search/results/people/?facetGeoRegion=%5B%22us%3A70%22%5D&origin=FACETED_SEARCH"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("button")
For Each AllFiltersButton In HTMLButtons
If AllFiltersButton.className = "search-filters-bar__all-filters button-tertiary-medium-muted flex-shrink-zero mr3" Then AllFiltersButton.Click
Next AllFiltersButton
Set HTMLInput = HTMLDoc.getElementById("search-advanced-company")
HTMLInput.Value = Company
Set HTMLInput = HTMLDoc.getElementById("search-advanced-title")
HTMLInput.Value = Position
Set ApplyButtons = HTMLDoc.getElementsByTagName("button")
For Each ApplyButton In ApplyButtons
If ApplyButton.className = "search-advanced-facets__button--apply button-primary-large" Then ApplyButton.Click
Next ApplyButton
I have all the necessary references selected (Microsoft HTML Object Library, Microsoft Internet Controls, Microsoft Office 15.0 Object Library etc.)
and this portion of the code seems to work flawlessly.
After spending hours trying to figure out a way to click in these search/filter boxes, I stumbled across a method which should work, but I can't seem to adapt the code for my specific circumstance.
Set evt = ie.Document.createEvent("keyboardevent")
evt.initEvent "change", True, False
PW.all(0).dispatchEvent evt
My attempt to adapt this method to click into the company search box (but doesn't work) is as follows:
Dim vSelect As HTMLSelectElement
Dim eventClick As Object
Set vSelect = HTMLDoc.getElementById("search-advanced-company")
Set eventClick = HTMLDoc.createEvent("click")
eventClick.initEvent "change", True, False
vSelect.dispatchEvent eventClick
How should I adapt this code to click into a search/filter box?
If any one could help me out in any way, it would be much appreciated.
Thanks in advance.
EDIT: I'm getting mixed information as to whether data scraping on LinkedIn is permitted or not. Just to clarify, I will not be using this code for data scraping to avoid any infringement issues.
After resorting to the often avoided and disrespected SendKeys function, I was finally able to make it work.
Below is the corrected code:
Sub Fill()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLButtons As MSHTML.IHTMLElementCollection
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLInputs As MSHTML.IHTMLElementCollection2
Dim AllFiltersButton As MSHTML.IHTMLElement
Dim ApplyButton As MSHTML.IHTMLElement
Dim ApplyButtons As MSHTML.IHTMLElementCollection3
Dim Company As String, Position As String
Company = Range("A2").Value
Position = ("""" + Range("B1").Value + """")
IE.Visible = True
IE.navigate "https://www.linkedin.com/search/results/people/?facetGeoRegion=%5B%22us%3A70%22%5D&origin=FACETED_SEARCH"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLButtons = HTMLDoc.getElementsByTagName("button")
For Each AllFiltersButton In HTMLButtons
If AllFiltersButton.className = "search-filters-bar__all-filters button-tertiary-medium-muted flex-shrink-zero mr3" Then AllFiltersButton.Click
Next AllFiltersButton
Set HTMLInputs = HTMLDoc.getElementsByTagName("input")
HTMLInputs(2).Value = Position
HTMLInputs(2).Focus
Application.SendKeys "{LEFT}", True
HTMLInputs(3).Value = Company
HTMLInputs(3).Focus
Application.SendKeys "{LEFT}", True
Set ApplyButtons = HTMLDoc.getElementsByTagName("button")
For Each ApplyButton In ApplyButtons
If ApplyButton.className = "search-advanced-facets__button--apply button-primary-large" Then ApplyButton.Click
Next ApplyButton
End Sub
A big "Thank you!" to #ashleedawg for the suggestion leading to the solution!

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