vba:如何在iframe中单击元素 [英] vba: How to click on element within iframe
问题描述
我的目标是单击html 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.
我尝试单击div ID,跨度标题,但到目前为止没有任何效果.我相信是因为错误的同步语
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:
- 单击选项卡7->这将打开正确的标签进行处理 来自"W"簇的inseart值等于0.转到全球工作组";字段
- 专注于全球工作组"字段
- 点击可验证全球工作组"的图片,场地 (验证插值) 来自"X"列的inseart值等于到工作组"字段
- 专注于工作组"字段
- 单击打开下拉选项的图像,该选项已生成 根据插入的值到工作组";字段
- 在iframe中,单击等于value的标题 插入到工作组"字段
- Click on tab 7 -> this will open the correct tab to work on
- inseart value from clumn "W" to "Global workgroup" field
- focus on "Global workgroup" field
- Click on an image that validate the "Global workgroup" field (validates the instered value)
- inseart value from clumn "X" to "Workgroup" field
- focus on "Workgroup" field
- Click on image that opens the drop down options, which is generated according the inserted value to "Workgroup" field
- Within the Iframe, Click on the title that is equal to value which was inserted to "Workgroup" field
我也尝试过使用Selenium IDE,以便可以看到录制的宏如何访问iframe并单击所需的元素:
I have also tried to use Selenium IDE so I can see how the recorded macro access the Iframe and click the desired elemnt:
- 命令:选择框架|目标:Index = 2
- 点击|目标:css = span [title =" APAC"]
我试图模仿VBE中的上述stpes,但是找不到正确编写它的方法.我曾尝试下载&应用selenium驱动程序并使用selenium库运行代码,但也被卡住了.
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.
下面的图片是iframe的html代码以及我要单击的所需元素:
Below image is the html code of the Iframe and the desired element I want to click on:
推荐答案
您应该能够使用以下语法
You should be able to use the following syntax
ie.document.querySelector("[id='_CPDDWRCC_ifr']").contentDocument.querySelector("span[title=APAC]").click
使用硒,您可以使用
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)
然后在contentDocument
上点击querySelector
.
这篇关于vba:如何在iframe中单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!