【问题标题】:Selenium VBA Excel - problem clicking a link within an iframeSelenium VBA Excel - 单击 iframe 中的链接时出现问题
【发布时间】:2020-07-09 18:37:16
【问题描述】:

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

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

我可以正常登录,并且可以使用FindElementByIDFindElementByTag("a").Click 语句在页面之间导航。

我在使用这种方法在一页上导航时遇到问题。我试过ByID(the first 2 IDs in the code below)ByLinkTextByClass (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>    

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

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

这是我尝试过的:

 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>

【问题讨论】:

  • 您是否收到错误消息?如果是这样,是什么?您是否检查过该元素不在框架/iframe 父级中?
  • @QHarr 你是对的。现在在 iframe 中我只能在错误前执行一个 `bot.FindElement SendKeys:bot.Wait 5000 bot.SwitchToFrame "mainPanel_iframe" bot.Wait 5000 bot.FindElementById("editConference_label").SendKeys "Test Jonathan" bot.FindElementById("editConference_start").SendKeys "30/03/2020" bot.FindElementById("editConference_startTime").SendKeys "17:00" bot.FindElementById("editConference_end").SendKeys "30/03/2020" bot.FindElementById("editConference_endTime").SendKeys "18:00"

标签: excel vba selenium xpath iframe


【解决方案1】:

要点击文本为安排会议的链接,您需要诱导一些等待,您可以使用以下任一Locator Strategies

  • 使用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
    

更新

如果所需的元素在 &lt;iframe&gt; 内,则首先需要 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
    

你可以在How to send text with in the username field within an iframe using Selenium VBA找到相关的详细讨论


参考

Ways to deal with #document under iframe

【讨论】:

  • 谢谢。我仍然遇到这种错误情况:运行时错误'7':NoSuchElementError Element not found for XPath=//a[@id='viewConferences_add' and contains(@href, 'addUserConference')]/span[@id= 'viewConferences_add_text']。也许我不在我认为我在的页面上,即使它正在显示?这是一种可能吗?我怎么知道?您能否建议如何通过 Get 语句直接转到页面引用。抱歉没有继续使用这个编辑器 - 你如何输入更多代码? J
  • @JonR 查看更新的答案并让我知道状态。
  • 是的。谢谢你。页面portal.starleaf.com/#page=addUserConference 加载。让我试着弄清楚如何获取数据。Best J
  • @QHarr 非常感谢。像往常一样,你又救了我们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 2018-05-31
  • 1970-01-01
相关资源
最近更新 更多