Slava Dolkart
Slava Dolkart

Reputation: 67

vba: How to click on element within iframe

My goal is to click an element within a html Iframe, but nothing worked for me so far. Hope someone can advise how to approach this task correctly as I am running in circles for weeks now.

I have tried to click on a div Id, span title but nothing worked so far. I believe it is because a wrong syntex

Option Explicit

Sub it_will_work()
'make the app work faster?
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'--------------------------------
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Fields") 'my data will be stored here

Dim LastRow As Long
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'range definition

Dim i As Long            'Will be used for a loop that navigate to different url
For i = 2 To LastRow     'First url starts at row 2 untill the last row

Dim IE As Object         'Internet Explorer declaration
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

IE.navigate sht.Range("A" & i).Value 'My url that I want to navigate to
While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
 
Dim Doc As New HTMLDocument 'Will be used for the main html page
Set Doc = IE.document
 
Doc.getElementById("tab7").Click  'data taht need to be updated is here

'Global workgroup data that will effect the workgroup data(dependency)
Doc.getElementById("mcdResourceGlobalWorkgroup_ddltxt").Value = sht.Range("W" & i).Value
Doc.getElementById("mcdResourceGlobalWorkgroup_ddltxt").Focus
Doc.getElementById("mcdResourceGlobalWorkgroup_ddlimg").Click

'Workgroup dropdown, that need to be choosen within the Iframe:
Doc.getElementById("ResourceWorkgroup").Value = sht.Range("X" & i).Value '1) worgroup that I want to insert
Doc.getElementById("ResourceWorkgroup").Focus
Doc.getElementById("_IB_imgResourceWorkgroup").Click  '2) Cliking here will generate dropdown values according the value inserted above

Application.Wait Now + TimeValue("00:00:5") 'before refering to Iframe I let the values to be loaded

'***from this point I have the issue where I try to access Iframe and click on the desired element:***
'Here I declare Iframe
Dim objIFRAME As Object
Set objIFRAME = IE.document.getElementsByTagName("iframe")
Debug.Print TypeName(objIFRAME)

'Here I ask to click on a title within the Iframe where value = X
objIFRAME.getElementsByName("title").Value = sht.Range("X" & i).Value.Click

Next i

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

After the url loads the following steps should happen:

  1. Click on tab 7 -> this will open the correct tab to work on
  2. inseart value from clumn "W" to "Global workgroup" field
  3. focus on "Global workgroup" field
  4. Click on an image that validate the "Global workgroup" field (validates the instered value)
  5. inseart value from clumn "X" to "Workgroup" field
  6. focus on "Workgroup" field
  7. Click on image that opens the drop down options, which is generated according the inserted value to "Workgroup" field
  8. Within the Iframe, Click on the title that is equal to value which was inserted to "Workgroup" field

I have also tried to use Selenium IDE so I can see how the recorded macro access the Iframe and click the desired elemnt:

  1. Command: Select frame | Target: Index=2
  2. Click | Target: css=span[title="APAC"]

I have tried to mimic the stpes above in VBE, but couldn't find a way to write it properly. I event tried to download & apply selenium driver and run the code using the selenium library but got stuck as well.

Below image is the html code of the Iframe and the desired element I want to click on: HTML code image

Upvotes: 1

Views: 2982

Answers (1)

QHarr
QHarr

Reputation: 84455

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.

Upvotes: 4

Related Questions