【问题标题】:vba: How to click on element within iframevba:如何单击 iframe 中的元素
【发布时间】:2019-06-01 07:04:42
【问题描述】:

我的目标是单击 html Iframe 中的元素,但到目前为止对我来说没有任何效果。希望有人能建议我如何正确地完成这项任务,因为我现在已经在圈子里跑了几个星期了。

我尝试单击 div Id、跨度标题,但到目前为止没有任何效果。我相信这是因为语法错误

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

网址加载后,应执行以下步骤:

  1. 单击选项卡 7 -> 这将打开正确的选项卡以进行处理
  2. 将值从“W”列插入“全局工作组”字段
  3. 专注于“全球工作组”领域
  4. 单击验证“全局工作组”字段的图像 (验证插入的值)
  5. 将值从“X”列插入“工作组”字段
  6. 关注“工作组”字段
  7. 点击图片打开下拉选项,生成 根据插入的值到“工作组”字段
  8. 在iframe中,点击与value相等的标题 已插入“工作组”字段

我也尝试过使用 Selenium IDE,这样我就可以看到录制的宏如何访问 Iframe 并单击所需的元素:

  1. 命令:选择框架 |目标:索引=2
  2. 点击 |目标:css=span[title="APAC"]

我试图在 VBE 中模仿上面的 stpes,但找不到正确编写它的方法。我尝试下载和应用 selenium 驱动程序并使用 selenium 库运行代码,但也卡住了。

下图是 Iframe 的 html 代码和我想要点击的元素:

【问题讨论】:

    标签: html vba selenium iframe web-scraping


    【解决方案1】:

    您应该能够使用以下语法

    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
    

    对于您现有的标签解决方案,您需要使用索引。例如,

    objIFRAME(0) 
    

    然后querySelector 上的contentDocument

    【讨论】:

    • QHarr,非常感谢(!)您建议的语法,它就像魔术一样!否则,我之前插入的所有值都不会保存,因为系统需要通过单击选择值。编辑:有没有办法为跨度标题创建一个可验证的值? : .querySelector("span[title=(sht.Range("X" & i).Value)]").Click
    • Qharr,有没有办法正确编写变量标题?所以标题是 sht.Range("X" & i).Value
    • 您不能使用从工作表 AFAIK 中读取的值来命名变量。
    • 您可以使用字典结构,例如:stackoverflow.com/q/38254337/6241235
    • 所以你认为没有合适的语法来做这样的事情: .querySelector("span[title=sht.Range("X" & i).Value]").Click BTw 这样做它弹出一个编译错误消息: Expected list separator or )
    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2020-11-04
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    相关资源
    最近更新 更多