如何在 VBA 中使用硒网络驱动程序单击框架内的按钮



我为一个有框架的网页做了一个自动化,我需要点击几个菜单链接,然后返回主内容来完成任务。此自动化从 Access-VBA 运行。下面是显示框架的 HTML 片段:

<frameset id="topFrameset"> 
  <frameset id="BodyFrameset" cols="0,177,*">
    <frame name="ButtonsFrame" id="ButtonsFrame" target="main" scrolling="NO" noresize="" src="MyMenu.action">
      #document
      <html>
      <body>
        <input type="hidden" name="listParentsId" value="[501, 502, 503]" id="listParentsId">
        <div id="menu_container">
          <div id="menu_1" onmouseover="MENU.ToolTipsCreate('501')">
            <a onclick="MENU.showItems('501', '')">My Account</a>
            <img id="img_501" src="/common/images/dropdown2_arrow.png" onclick="MENU.showItems('501', '')">
          </div>
        </div>		
      </body></html>
    </frame>
  </frameset>
</frameset>

我正在使用 SeleniumBasic v2.0.9.0 绑定进行 VBA。请参阅使用 Selenium 在 Excel VBA 中进行的浏览器自动化安装和教程。

我试过网络驱动程序。SwitchToFrame,在许多其他测试中,所有这些都失败了。

Dim driver As selenium.WebDriver
Set driver = New ChromeDriver
driver.Get (URL)
'Here goes the part of the code that logs in the page, it's irrelevant for this analysis...
driver.SwitchToFrame ("topFrameset") 'This line returns Error N°: 8 - NoSuchFrameError. Frame not be found. Identifier:topFrameset

希望有人能指导我单击"menu_1"中的链接或 img 元素。提前谢谢。

在"示例测试 2"中,您能否driver.SwitchToDefaultContent注释此行并尝试一下?为什么在切换到帧之前切换到默认内容?

默认内容 –> 切换回默认页面

也可以尝试其他选项:(在 JAVA 中,我们这样做。

switchTo().frame(int) – Switch to a frame with its index switchTo().frame(“name of the frame”) – Switch to a frame with its name switchTo().frame (WebElement) – Switch to a frame with its web element location

在为此做了很多工作之后,我自己找到了解决方案。我安装了谷歌商店中可用的应用程序"SideeX"。然后我记录了我需要用硒模仿的动作。然后,我注意到我在原始问题中描述的名称(topFrameset,BodyFrameset,ButtonsFrame)由SideeX通过其索引记录。所以我只需要对硒执行相同的操作。例如,在我的情况下,我有以下一系列操作:

driver.SwitchToFrame 0
driver.SwitchToFrame 1
driver.FindElementById("img_501").Click
driver.SwitchToParentFrame
driver.SwitchToFrame 2

这只是我的情况,但对于遇到相同问题的其他人有帮助的重要一点是,您可以使用SideeX来帮助您找到实现目标所需的元素索引或操作顺序。

最新更新