【问题标题】:VBA- wanted to click element which has multiple framesVBA-想要单击具有多个框架的元素
【发布时间】:2020-11-07 02:54:25
【问题描述】:

根据上一个问题的答案,我向前迈进了一步,但仍然停留在我想单击元素的地方,但它似乎有 2 帧,这就是我运行代码时给出的错误该元素未找到的原因,请检查附件和下面的代码和建议。

Sub activeBexIE_Final()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'On Error Resume Next
Dim Perm_bot As New Selenium.IEDriver         
  
Perm_bot.Get "official link"

Perm_bot.Wait 2000

Perm_bot.FindElementById("logonuidfield").SendKeys "XYZ"
Perm_bot.SendKeys Perm_bot.Keys.Tab
Perm_bot.SendKeys "PQR"
Perm_bot.SendKeys Perm_bot.Keys.Enter
Perm_bot.Wait 40000

Perm_bot.switchToFrame 
Perm_bot.FindElementById("iframe_Roundtrip_9223372036563636042")
Perm_bot.FindElementById("BUTTON_OPEN_SAVE_btn1_acButton").SendKeys 
Perm_bot.Keys.Enter
Perm_bot.Wait 30000' *till here I am sucessful- Opening Url-login-click on 
"Open" option*

*'from here I am unable to move further, pls check attachment"select 
layout" like how I wanted to select and also check the attachment "frame" 
and "element" which is having the HTML details, not sure if this particular 
element has 2 element, if yes then how I should write the script and after 
this I wanted to do  tab 7 times and then enter, kindly help for further 
script, thanks in advance

'Perm_bot.switchToFrame Perm_bot.FindElementById("urPopupOuter0")

Perm_bot.switchToFrame Perm_bot.FindElementById("urPopupInner0")

Perm_bot.FindElementById("LOAD_state_tigen4_tlv1_list_unid6_tv").Click

SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{ENTER}"
    
Perm_bot.Wait 10000
  
Application.DisplayAlerts = True
Application.ScreenUpdating = True
 
    'perm_dot.Quit
'Set perm_dot = Nothing
    
End Sub

Select Layout FrameElement

【问题讨论】:

    标签: vba selenium internet-explorer web-scraping iframe


    【解决方案1】:

    我相信您担心的是您的 iframe 具有相似的属性,您必须在 tab 7 次之前切换到其中的每一个。

    方法是 就像获取所有窗口句柄然后循环一样,您可以为 iframe 使用通用 xpath,例如

       findelements(By.Xpath("//iframes))
    

    这将返回 iframe 元素,然后您可以使用循环和遍历并使用

    SwitchToFrame().

    【讨论】:

      【解决方案2】:

      如果您的网页包含多个框架并且您还需要对每个框架执行操作,那么我建议尝试为这些框架创建一个对象并将其分配给“文档”对象,然后尝试对每个框架执行操作框架。

      Sub demo()
          
          Dim URL As String
          Dim IE, doc1, doc2 As Object
          Dim frame1 As HTMLFrameElement
          Dim frame2 As HTMLFrameElement
         
          Set IE = CreateObject("InternetExplorer.Application")
         
          IE.Visible = True
          
          URL = "D:\Tests\parent.html"
       
          IE.Navigate URL
        
          Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
          Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until
       
          Set frame1 = IE.document.getElementByID("urpopupouter0")
          Set frame2 = IE.document.getElementByID("urpopupinner0")
          Set doc1 = frame1.contentWindow.document
          Set doc2 = frame2.contentWindow.document
          IE.document.getElementByID("btn1").Click
          doc1.getElementByID("btn1").Click
          doc2.getElementByID("btn1").Click
          
          Set IE = Nothing
         
      End Sub
      

      我在我这边测试了上面的代码,它适用于网页上的多个框架。

      测试结果:

      VBA 代码正在单击每个框架和父网页上的按钮,这会将按钮的前景色更改为红色。

      此外,您可以尝试根据自己的要求修改代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 2017-07-29
        • 2019-12-02
        相关资源
        最近更新 更多