How to deal with Hidden Elements on VBA Selenium [duplicate] - html

I am trying to automate a report through VBA. I have worked in VBA but not able to login in iTunes website through codes. Someone told me that it is written in IFrame, but i have no idea. Even i am not able to put my username in input box of login page.
https://itunesconnect.apple.com/login
Dim HTMLdoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub check()
Dim MyHTML_element As IHTMLElement
Dim MyURL As String
MyURL = "https://itunesconnect.apple.com/login"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLdoc = MyBrowser.document
HTMLdoc.getElementsByID("account_name_text_field").Value = "username#outlook.com"
HTMLdoc.all.Password.Value = "password"
For Each MyHTML_element In HTMLdoc.getElementsByTagName("input")
If MyHTML_element.Type = "sign-in" Then MyHTML_element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

API sounds like a good idea. The whole page is very slow loading (for me at least) and there is an iframe to navigate.
I would go with selenium basic wrapper for vba and switch to the iframe. I will try to improve this when I have time but for now this works.
After installing selenium you will need to add a reference via VBE > Tools > References to Selenium Type library.
Option Explicit
Public Sub EnterInfo()
Dim d As WebDriver, t As Date, ele As Object
Set d = New ChromeDriver
Const URL = "https://itunesconnect.apple.com/login"
Const WAIT_TIME_SECS As Long = 30
t = Timer
With d
.Start "Chrome"
.get URL
Do
DoEvents
If Timer - t > WAIT_TIME_SECS Then Exit Do
On Error Resume Next
.SwitchToFrame "aid-auth-widget-iFrame"
Set ele = .FindElementByCss("#account_name_text_field")
On Error GoTo 0
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
ele.SendKeys "Joe.Bloggs#aol.com"
.FindElementByCss("#sign-in").Click
'Other code....
Stop '<=Delete me later
.Quit
End With
End Sub

Related

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

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.

How to resolve runtime error when trying to execute Element.Click command

I am attempting to run a macro that clicks on an IE webpage to download a file. When running my macro I encounter Runtime Error 91: Object variable or with block variable not set I removed my url string for security purposes
The line that is highlighted is Element.Click How can I resolve this
Sub Playthissub()
Call Scrape("myurl") '' change the website here and execute this sub.
End Sub
Sub Scrape(url As String)
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As HTMLGenericElement
Set Browser = New InternetExplorerMedium
Browser.Visible = True
Browser.Navigate url
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Set Elements = Document.getElementsByTagName("navPipeline")
Element.Click
Browser.Quit
End Sub

Vba Web Navigation and authentication

Sub HTML_Element_Printer()
On Error GoTo Err_Clear
sURL = "https://ernie.erau.edu/"
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.loginID.Value = "User123"
HTMLDoc.all.Password.Value = "Pass123"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
Debug.Print oHTML_Element.Name
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
I am trying to use vba to navigate through multiple websites and login to it respectively, thus, i am using my school portal as a trial for now. The code seems to work successfully in navigating to the site, but i can't input the user and password i want. It is the first time i am using vba to do web navigation and user authentication, i was hoping to find some help and advice here. Thanks.

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