1

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

Community
  • 1
  • 1
Slava Dolkart
  • 67
  • 1
  • 2
  • 9

1 Answers1

4

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.

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • QHarr, thanks alot(!) for your advised syntax, it worked like magic! Without that, all the values I have inserted before won't be saved as the system requires choosing value by click. Edit: Is there a way to make a veriable value for the span title? : .querySelector("span[title=(sht.Range("X" & i).Value)]").Click – Slava Dolkart Jan 06 '19 at 11:33
  • Qharr, is there a way to write properly a variable title? so the title would be sht.Range("X" & i).Value – Slava Dolkart Jan 06 '19 at 12:00
  • You cannot name a variable using a value read in from the sheet AFAIK. – QHarr Jan 06 '19 at 12:02
  • You could use a dictionary structure for example: https://stackoverflow.com/q/38254337/6241235 – QHarr Jan 06 '19 at 12:03
  • So you reckon there is no proper syntax for doing something like that: .querySelector("span[title=sht.Range("X" & i).Value]").Click BTw doing that it pop up a compile error message: Expected list seperator or ) – Slava Dolkart Jan 06 '19 at 12:14
  • Oh yes you can absolutely do that. Just change your inner “ to ‘ – QHarr Jan 06 '19 at 12:21
  • .querySelector('span[title=sht.Range("X" & i).Value]').Click This way not working, if you meant ("span[title=sht.Range('X' & i).Value]").Click it has a compile error aswell. could you please adress what am I doing wrong? or perhaps I didnt get which inner " change to ' – Slava Dolkart Jan 06 '19 at 12:33
  • The inner. The ones around X, not the outer. – QHarr Jan 06 '19 at 12:33
  • .querySelector("span[title=sht.Range('X' & i).Value]").Click >> I recied run-time-error "Method 'querySelector' of object 'JScriptTypeInfo' failed. Perhaps it should be somewhere else? – Slava Dolkart Jan 06 '19 at 12:42