首页 > 解决方案 > Selenium VBA Excel - 单击 iframe 中的链接时出现问题

问题描述

我是论坛的新手,有一些 VBA 经验,但不是很多。

我正在使用 Selenium 来促进与 Chrome 站点的交互,目的是使用 Excel 电子表格中的数据自动向站点(Starleaf Web 会议)发出一系列请求。

FindElementByID我可以很好地登录,并且可以使用和FindElementByTag("a").Click语句从一个页面导航到另一个页面。

我在一个页面上使用这种方法导航时遇到问题。我试过了ByID(the first 2 IDs in the code below),,,,ByLinkText;但没有任何效果。此表格的代码行上的宏停顿:ByClass (portalContent, button, single)ByCssSelector

Set elem = bot.FindElementBy*****("Parameter")
    elem.FindElementByTag("a").Click

有问题的页面上的代码是:

<div class="portalContent">
    <div id="viewConferencesAddDiv" class="buttons single" style="">
        <a id="viewConferences_add" href="https://portal.starleaf.com/#page=addUserConference" target="_parent">
            <span id="viewConferences_add_text">Schedule meeting</span>
        </a>
    </div>    

我想知道id="viewConferences_add"'a'标签和href之间的问题是否可能是问题,但我不知道如何解决这个问题。

我尝试使用的另一种方法是在登录后直接转到我需要的页面,但这似乎不起作用。我可能有语法问题???

这是我尝试过的:

 bot.Get "/#page=addUserConference"
 bot.Get "https://portal.starleaf.com/#page=addUserConference"
 bot.Get "//portal.starleaf.com/#page=addUserConference"

之后使用此代码允许页面加载时间:

 Do Until InStr(1, bot.Url, "addUserConference") > 0
 Application.Wait (DateAdd("s", 1, Now))
 DoEvents
 Loop

更新

我环顾四周,发现有人在谈论 iframe 切换。查看 F12 中的页面,实际上看起来“安排会议”按钮可能位于 iframe 中。建议是切换到 if 但是我不知道该怎么做。页面上似乎有几个 iframe - 所以我想我需要 ID。我认为它可能是 id=mainPanel_iframe。

iframe 标签的 HTML:

<iframe id="mainPanel_iframe" name="mainPanel_iframe" class="mochaIframe" src="https://portal.starleaf.com/pbx/node-1.live.pr.starleaf.com/portal/#page=users&amp;lang=en_int" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" style="height: 579px; width: 1543px;"></iframe>

标签: excelvbaseleniumxpathiframe

解决方案


要单击带有文本作为安排会议的链接,您需要等待一段时间,您可以使用以下任一定位器策略

  • 使用FindElementByCss

    bot.wait 5000
    bot.FindElementByCss("a#viewConferences_add[href*='addUserConference'] > span#viewConferences_add_text").Click
    
  • 使用FindElementByXPath

    bot.wait 5000
    bot.FindElementByXPath("//a[@id='viewConferences_add' and contains(@href, 'addUserConference')]/span[@id='viewConferences_add_text']").Click
    

更新

如果所需的元素在<iframe>第一个之内,则需要执行SwitchToFrame以下操作:

  • 使用FindElementByCss

    bot.wait 5000
    bot.SwitchToFrame "mainPanel_iframe"
    bot.wait 5000
    bot.FindElementByCss("a#viewConferences_add[href*='addUserConference'] > span#viewConferences_add_text").Click
    
  • 使用FindElementByXPath

    bot.wait 5000
    bot.SwitchToFrame "mainPanel_iframe"
    bot.wait 5000
    bot.FindElementByXPath("//a[@id='viewConferences_add' and contains(@href, 'addUserConference')]/span[@id='viewConferences_add_text']").Click
    

您可以在如何使用 Selenium VBA 在 iframe 内的用户名字段中发送文本中找到相关的详细讨论


参考

iframe下处理#document的方法


推荐阅读