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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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
Qharr, is there a way to write properly a variable title? so the title would be sht.Range("X" & i).Value
You cannot name a variable using a value read in from the sheet AFAIK.
You could use a dictionary structure for example: stackoverflow.com/q/38254337/6241235
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 )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.