For some reason I am not able to access elements inside iframe.
I have tried:
ie.Document.getElementById("sisaltosivu").contentDocument.getElementById("uusihaku")(0).Click
Here is my code:
Sub ChechAutomate()
Dim ie As New InternetExplorer, url As String, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Other Data")
url = "https://www.asiakastieto.fi/data/atdb"
With ie
.Visible = True
.Navigate2 url
While .Busy Or .ReadyState < 4: DoEvents: Wend
With .Document
If .querySelectorAll("#btnb").Length > 0 Then
Else
.querySelector("[name=user1]").Value = "x"
.querySelector("[name=user2]").Value = "x"
.querySelector("[name=passwd]").Value = "x"
.querySelector("[class=btnb]").Click
Application.Wait (Now + TimeValue("00:00:01"))
ie.Navigate2 "https://www.asiakastieto.fi/sopimusasiakas/kotimaa?lang=FI"
Application.Wait (Now + TimeValue("00:00:01"))
ie.Document.getElementById("sisaltosivu").contentDocument.getElementById("uusihaku")(0).Click
End If
End With
End With
End Sub
You are no longer outside the iframe when you navigate to the src of the iframe. This line:
ie.Navigate2 "https://www.asiakastieto.fi/sopimusasiakas/kotimaa?lang=FI"
should mean you are now in the document referenced within the iframe and should access direct with:
.document.getElementById("uusihaku")(0).Click
Related
Is a long time ago to programming VBA. I want to fetch in a web Table all Linked PDF Files in coll um (x). First I have created the logins and navigate to the site to fetch file.
Site table:
Sub goToShopWH()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim clip As Object
'Table Grapping
Dim ieTable As Object
'create a new instance of ie
'Set ieApp = New InternetExplorer
Set ieApp = CreateObject("InternetExplorer.Application")
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://website.com/ishop/Home.html"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents:
Loop
' Login to site
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc '.forms("loginForm_0")
.getElementById("loginField").Value = "username"
.getElementById("password").Value = "Password"
.getElementById("loginSubmit").Click
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
'Switsh to search form
ieApp.Navigate "https://website.com/ishop/account/MyAccount,$comp$account$AccountNavigation.orderHistory.sdirect?sp=Saccount%2FOrderHistory"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
' fillout form
Set ieDoc = ieApp.Document
With ieDoc '.forms("Form_0")
.getElementById("PropertySelection_2").Value = "7"
.getElementById("commtxt").Value = "190055" 'Projekt Nummer oder Text.
.getElementById("Submit_0").Click
End With
Do
DoEvents
Loop Until ieApp.ReadyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)
With ieDoc '.forms("Form_1")
.getElementById("PropertySelection").Value = "3"
.getElementById("PropertySelection").FireEvent ("onchange")
End With
Do
DoEvents
Loop Until ieApp.ReadyState = 4
Application.Wait Now + TimeSerial(0, 0, 5)
End With
Set webpage = ieApp.Document
Set table_data = webpage.getElementsByTagName("tr")
End Sub
Please help my to solve this to get I want import Table to sheet2 and download all PDF in ("Table"),("tbody")(tr)(1 to X),(td)(10),(href).click
I think you are after something like this.
Sub webpage()
Dim internet As Object
Dim internetdata As Object
Dim div_result As Object
Dim header_links As Object
Dim link As Object
Dim URL As String
Set internet = CreateObject("InternetExplorer.Application")
internet.Visible = True
URL = "https://www.google.co.in/search?q=how+to+program+in+vba"
internet.Navigate URL
Do Until internet.ReadyState >= 4
DoEvents
Loop
Application.Wait Now + TimeSerial(0, 0, 5)
Set internetdata = internet.Document
Set div_result = internetdata.getelementbyid("res")
Set header_links = div_result.getelementsbytagname("h3")
For Each h In header_links
Set link = h.ChildNodes.Item(0)
Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href
Next
MsgBox "done"
End Sub
Or, perhaps this.
' place your URL in cell L1
Sub HREF_Web()
Dim doc As HTMLDocument
Dim output As Object
Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("L1")
Do
'DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
Set output = doc.getElementsByTagName("a")
i = 5
For Each link In output
'If link.InnerHTML = "" Then
Range("A" & i).Value2 = link
' End If
i = i + 1
Next
MsgBox "Done!"
End Sub
I've created this code that simulates my actual application. And I'm trying to run the procedure through WebBrowser1. If in my actual application it was possible to use .document.getElementById would work, but you need .getElementsByClassName or TagName. How could I do this with the data below?
The code application is executed by the CommandButton and the WebBrowser will be inside a Userform.
Private Sub CommandButton1_Click()
Dim IE As InternetExplorer
Dim iELE As Object
Dim xUser As String
Dim xPswd As String
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate ("https://twitter.com/login?lang=pt")
xUser = "your username"
xPswd = "your password"
Application.Wait (Now + TimeValue("0:00:10"))
Do While IE.Busy = True Or IE.ReadyState <> 4: DoEvents: Loop
For Each iELE In IE.Document.getElementsByClassName("js-username-field email-input js-initial-focus")
If iELE.Value = "" Then
iELE.Value = xUser
End If
Exit For
Next
For Each iELE In IE.Document.getElementsByClassName("js-password-field")
If iELE.Value = "" Then
iELE.Value = xPswd
End If
Exit For
Next
Application.Wait (Now + TimeValue("0:00:05"))
For Each iELE In IE.Document.getElementsByClassName("submit EdgeButton EdgeButton--primary EdgeButtom--medium")
If iELE.Value = "" Then
iELE.Click
End If
Exit For
Next
End Sub
Try this
Sub Login(MyLogin, MyPass, strUrl)
Dim IeApp As InternetExplorer
Dim IeDoc As Object
Dim ieTable As Object
Set IeApp = New InternetExplorer
IeApp.Visible = False
IeApp.Navigate strUrl
Do While IeApp.Busy: DoEvents: Loop
Do Until IeApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = IeApp.Document
With IeDoc
.getElementsByClassName("js-username-field")(0).value = MyLogin
.getElementsByClassName("js-password-field")(0).value = MyPass
End With
IeDoc.getElementsByClassName("submit")(0).Click
Do While IeApp.Busy: DoEvents: Loop
Do Until IeApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
End Sub
https://www.pk-anexcelexpert.com/vba-web-browser-in-the-user-form/
I have the following in order to get data from webpage and I arrange them per column in a worksheet. I take one of the data which is a URL and after I put them in cells I want to navigate again to that page and get my last info.
Option Explicit
Public Sub GetInfo()
Dim ie As InternetExplorer: Set ie = New InternetExplorer
Dim i As Long
Const MAX_WAIT_SEC As Long = 20
With ie
.Visible = True
.Navigate2 "https://www.skroutz.gr/s/8988836/Mattel-Hot-Wheels-%CE%91%CF%85%CF%84%CE%BF%CE%BA%CE%B9%CE%BD%CE%B7%CF%84%CE%AC%CE%BA%CE%B9%CE%B1-%CE%A3%CE%B5%CF%84-%CF%84%CF%89%CE%BD-10.html"
While .Busy Or .ReadyState < 4: DoEvents: Wend
Dim finalPrices As Object, sellers As Object, availability As Object
Dim products As Object, t As Date
Set products = .Document.querySelectorAll(".card.js-product-card")
t = Timer
Do
DoEvents
ie.Document.parentWindow.execScript "window.scrollBy(0, window.innerHeight);", "javascript"
Set finalPrices = .Document.querySelectorAll(".card.js-product-card span.final-price")
Application.Wait Now + TimeSerial(0, 0, 1)
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop Until finalPrices.Length = products.Length
Set sellers = .Document.querySelectorAll(".card.js-product-card .shop.cf a[title]")
Set availability = .Document.querySelectorAll(".card.js-product-card span.availability")
With ThisWorkbook.Worksheets("TESTINGS")
For i = 0 To 5
If availability.Item(i).innerText = "Άμεσα Διαθέσιμο σε 1 έως 3 ημέρες" Then
.Cells(2, i + 4) = sellers.Item(i)
.Cells(3, i + 4) = finalPrices.Item(i).innerText
.Cells(4, i + 4) = availability.Item(i).innerText
End If
Next
.Columns("D:I").AutoFit
ie.Quit
'Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
'DoEvents
'Loop
'Dim place As Object, mylink As String
'For i = 0 To 5
' ie.Visible = True
' mylink = .Cells(2, i + 4).Value
' If mylink <> "" Then
' ie.Navigate2 mylink
' Set place = ie.Document.querySelector(".shop-stores.cf")
' .Cells(5, i + 4) = place.innerText
' End If
'Next
End With
End With
End Sub
If I add the following which checks the cell with a URL, and if it has value inside then opens the URL gets the value and finish then I get Automation Error
Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
Dim place As Object, mylink As String
For i = 0 To 5
ie.Visible = True
mylink = .Cells(2, i + 4).Value
If mylink <> "" Then
ie.Navigate2 mylink
Set place = ie.Document.querySelector(".shop-stores.cf")
.Cells(5, i + 4) = place.innerText
End If
Next
Then I get an Automation error on the following line
Set place = ie.Document.querySelector(".shop-stores.cf")
Can't go from one page to another?
Do I have to create a different sub and call it from GetInfo() sub?
You need a proper wait after each navigate2 and make your ie object visible outside of all loops e.g. structure. You could add in a timed loop for setting of place variable. I have added a safeguard test of If Not Is Nothing. Have your .Quit at the end after you have finished with the ie object.
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer, i As Long
Const MAX_WAIT_SEC As Long = 20
With ie
.Visible = True
.Navigate2 "https://www.skroutz.gr/s/8988836/Mattel-Hot-Wheels-%CE%91%CF%85%CF%84%CE%BF%CE%BA%CE%B9%CE%BD%CE%B7%CF%84%CE%AC%CE%BA%CE%B9%CE%B1-%CE%A3%CE%B5%CF%84-%CF%84%CF%89%CE%BD-10.html"
While .Busy Or .readyState < 4: DoEvents: Wend
'code with first link
Dim place As Object, mylink As String
For i = 0 To 5
mylink = ActiveSheet.Cells(2, i + 4).Value
If mylink <> vbNullString Then
.Navigate2 mylink
While .Busy Or .readyState < 4: DoEvents: Wend
On Error Resume Next
Set place = .document.querySelector(".shop-stores.cf")
On Error GoTo 0
If Not place Is Nothing Then
ActiveSheet.Cells(5, i + 4) = place.innerText
Set place = Nothing
End If
End If
Next
.Quit
End With
End Sub
I would like to extract the hyperlink from a webpage by using queryselector all, but there are no results coming out.
Below is my code.
Sub ScrapLink()
Application.ScreenUpdating = False
Dim IE As New InternetExplorer, html As HTMLDocument
Dim x As Long
Application.ScreenUpdating = False
With IE
IE.Visible = True
IE.Navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .ReadyState < 4: DoEvents: Wend
Application.Wait Now + TimeSerial(0, 0, 1)
DoEvents
With .Document.getElementById("bm_ann_detail_iframe").contentDocument
Dim links As Object, i As Long
Set links = .Document.querySelectorAll("p.att_download_pdf[href^='/FileAccess/apbursaweb/']")
For i = 1 To links.Length
With ThisWorkbook.Worksheets("Sheet1")
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = links.Item(i - 1)
End With
Next i
.Quit
End With
End With
End Sub
You could just avoid the initial page and use the URL direct from the frame. This would be my preference unless you don't know, for some reason, this URL.
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, nodeList As Object, i As Long
With IE
.Visible = True
.navigate2 "http://disclosure.bursamalaysia.com/FileAccess/viewHtml?e=2906127"
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
For i = 0 To nodeList.Length - 1
Debug.Print nodeList.item(i).href
Next
.Quit
End With
End Sub
Or you can jump right on over to the iframe src after page load:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, nodeList As Object, i As Long
With IE
.Visible = True
.Navigate2 "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .readyState < 4: DoEvents: Wend
.Navigate2 .document.querySelector("iframe").src
While .Busy Or .readyState < 4: DoEvents: Wend
Set nodeList = .document.querySelectorAll(".att_download_pdf [href^='/FileAccess/apbursaweb/download']")
For i = 0 To nodeList.Length - 1
Debug.Print nodeList.item(i).href
Next
.Quit
End With
End Sub
Try the following. It should fetch you the links you wish to grab:
Sub ScrapLink()
Dim IE As New InternetExplorer, Html As HTMLDocument
Dim frame As Object, i As Long
With IE
.Visible = True
.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5978065"
While .Busy Or .readyState < 4: DoEvents: Wend
Set Html = .document
End With
Application.Wait Now + TimeValue("00:00:03") 'This delay may vary in your case
Set frame = Html.getElementById("bm_ann_detail_iframe").contentWindow.document
With frame.querySelectorAll("p.att_download_pdf a")
For i = 0 To .Length - 1
Cells(i + 1, 1) = .item(i).getAttribute("href")
Next i
End With
End Sub
If you wish to kick out the delay then try changing the portion below with the above one:
Do: Set frame = Html.getElementById("bm_ann_detail_iframe"): DoEvents: Loop While frame Is Nothing
With frame.contentWindow.document.querySelectorAll("p.att_download_pdf a")
For i = 0 To .Length - 1
Cells(i + 1, 1) = .item(i).getAttribute("href")
Next i
End With
Quick question I hope there is an easy answer.
I have a sub routine that navigates through a webpage. "Sub Navigate()"
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
and so on.....
This part works fine and I successfully navigate through IE as needed. My next step comes from a separate sub routine "Sub copyimage()" I have designed where I pull images off the active page. However I cannot figure out how to use the open webpage for this.
The "Sub Copyimage()" sub works if I navigate directly to the URL within this sub. But I want to be able to get there from the work of my "Sub Navigate()" code.
I would like to be able to call the Copyimage() subroutine from the navigate() subroutine.
Please help
Here is full code from the first sub:
Sub FMSWebsite()
Dim strURL
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
strURL = "https://functionalmovement.com/login?return=%2F"
ie.Navigate strURL
ie.Visible = True
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
ie.Document.forms(0).all("Username").Value = Worksheets("Cover").Range("E8")
ie.Document.forms(0).all("Password").Value = Worksheets("Cover").Range("E10")
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
ie.Document.forms(0).submit
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Application.Wait (now + TimeValue("0:00:01"))
ie.Navigate ("http://functionalmovement.com/pro/clients")
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
ie.Document.forms(1).all("gvClients$DXFREditorcol1").Value = Worksheets("Template").Range("X2")
ie.Document.forms(1).all("gvClients$DXFREditorcol1").Select
SendKeys String:="{enter}", Wait:=True
Application.Wait (now + TimeValue("0:00:03"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Dim e
For Each e In ie.Document.getElementsByTagName("strong")
If e.innerText = "Client Workouts" Then
e.ParentElement.Click
Exit For
End If
Next
Application.Wait (now + TimeValue("0:00:01"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "Create a New Workout" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Application.Wait (now + TimeValue("0:00:01"))
For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "NEXT" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Application.Wait (now + TimeValue("0:00:01"))
Set goBtn = ie.Document.getElementById("newscreen")
goBtn.Click
Application.Wait (now + TimeValue("0:00:01"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Fast Forward some code it ends like so:
For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "Complete" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
Application.Wait (now + TimeValue("0:00:03"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "View Assigned Workout" Then
e.ParentElement.Click
Exit For
End If
Next
End Sub
My next sub starts with
Sub findimageA ()
For Each Elem In ie.Document.getElementsByTagName("img")
If Elem.getAttribute("title") = "Step 1" Then
How do I reference the web address I ended with in the first sub?
I believe you have in Navigate() something like:
Dim ieDocument as Object
which represent a web page (web document). If so, you need to create Copyimage() subroutine with argument, like this:
Sub Copyimage(ieDocImages as object)
and call this sub from previous one passing document (ieDocument) as a parameter:
Call Copyimage(ieDocument)
or do something similar depending of variables you use.
EDIT, based on additional code in question
First sub- add the following line before End Sub or before you terminate ie object:
Call findimagA(ie.Document)
and change the second sub as follows:
Sub findimageA(ieDocument As Object)
For Each Elem In ieDocument.getElementsByTagName("img")
Not tested but I'm sure it is ok.