Accessing object in iframe using VBA
It is sometimes tricky with iframes
. Based on html
you provided I have created this example. Which works locally, but would it work for you as well?
To get to the IFrame
the frames
collection can be used. Hope you know the name
of the IFrame
?
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.frames("iframename").document
Then to go the the image
we can use querySelector
method e.g. like this:
Dim img As MSHTML.HTMLImg
Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://stackoverflow.com'] img")
The selector a[href^='https://stackoverflow.com']
selects anchor
which has an href
attribute which starts with given text. The ^
denotes the beginning.
Then when we have the image just a simple call to click
on its parent which is the desired anchor
. HTH
Complete example:
Option Explicit
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
Sub Demo()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim url As String
url = "file:///C:/Users/dusek/Documents/My Web Sites/mainpage.html"
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate url
While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set doc = ie.document
Dim iframeDoc As MSHTML.HTMLDocument
Set iframeDoc = doc.frames("iframename").document
If iframeDoc Is Nothing Then
MsgBox "IFrame with name 'iframename' was not found."
ie.Quit
Exit Sub
End If
Dim img As MSHTML.HTMLImg
Set img = iframeDoc.querySelector("div table[id='table1'] tbody tr td a[href^='https://stackoverflow.com'] img")
If img Is Nothing Then
MsgBox "Image element within iframe was not found."
ie.Quit
Exit Sub
Else
img.parentElement.Click
End If
ie.Quit
End Sub
Main page HTML used
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>x -</title>
</head>
<body>
<iframe name="iframename" src="iframe1.html">
</iframe>
</body>
</html>
IFrame HTML used (saved as file
iframe1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- saved from url=(0016)http://localhost -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>
<body>
<div align="center">
<table id="table1" style="border-collapse: collapse" width="700" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td colspan="6">
<a href="https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba">
<img src="img.gif" width="38" height="38" border="0" align="right">
</a>
<strong>x - </strong>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
BTW, The frame may be referenced by it's index also
doc.frames(0).document
. Thanks to Paulo Bueno.
How to fetch iframe data using Excel VBA
You can change the line of extract "trex" element to one of the following, both of them can work well:
Use the
getElementsbyTagName
method to get the Iframe first , then according to theIframe.contentDocument
property to reach the element via the class name:productDesc = Docx.getElementsByTagName("iframe")(0).contentDocument.getElementsByClassName("trex")(0).outerHTML
Use
querySelector
method to get the Iframe through class, then use the same as the above to reach the element:productDesc = Docx.querySelector(".section-iframe-iframe").contentDocument.getElementsByClassName("trex")(0).outerHTML
Excel VBA: Working with iFrame via IE Automation
You try to use the following code to get elements from the iframe:
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
Sample code as below:
index page:
<input id="txtinput" type="text" /><br />
<input id="btnSayHello" type="button" value="Say Hello" onclick="document.getElementById('result').innerText = 'Hello ' + document.getElementById('txtinput').value" /><br />
<div id="result"></div><br />
<iframe width="500px" height="300px" src="vbaiframecontent.html">
</iframe>
vbaframeContent.html
<input id="txtcontentinput" type="text" /><br />
<input id="btncontentSayHello" type="button" value="Say Hello" onclick="document.getElementById('content_result').innerText = 'Hello ' + document.getElementById('txtcontentinput').value" /><br />
<div id="content_result"></div>
The VBA script as below:
Sub extractTablesData1()
'we define the essential variables
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("<your website url>")
While IE.ReadyState <> 4
DoEvents
Wend
Set Data = IE.Document.getElementsbyTagName("input")
'Navigating to the page I need help with that contains the iFrame structure.
IE.Document.getElementbyId("txtinput").Value = "AAA"
IE.Document.getElementbyId("btnSayHello").Click
'Waiting for the site to load.
'loadingSite
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("txtcontentinput").Value = "BBB"
IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementbyId("btncontentSayHello").Click
End With
Set IE = Nothing
End Sub
After running the script, the result as below:
vba: How to click on element within iframe
You should be able to use the following syntax
ie.document.querySelector("[id='_CPDDWRCC_ifr']").contentDocument.querySelector("span[title=APAC]").click
With selenium you can use
driver.SwitchToFrame driver.FindElementByCss("[id='_CPDDWRCC_ifr']")
driver.FindElementByCss("span[title=APAC]").click
With your existing tag solution you need to use an index. For example,
objIFRAME(0)
Then querySelector
on the contentDocument
of that.
Related Topics
Why Is There a Vertical Scroll Bar If Parent and Child Have the Same Height
How to Position Two Elements Side by Side Using Css
Soft Hyphen in HTML (<Wbr> VS. &Shy;)
Localization of Input Type Number
Android: How to Use the HTML.Taghandler
CSS Float: Floating an Image to the Left of the Text
Creating a CSS3 Box-Shadow on All Sides But One
How to Add Hamburger Menu in Bootstrap
Hide Textfield Blinking Cursor
Can an HTML Element Have the Same Attribute Twice
What Is Use of 'Initial' Value in Css
Custom Bullet Symbol for <Li> Elements in <Ul> That Is a Regular Character, and Not an Image
HTML - Links Without Http Protocol
How to Hide Elements Without Having Them Take Space on the Page
How to Display Vertical Text in Table Headers with Auto Height/Without Text Overflow