在量角测试中找不到iframe



我正在尝试找到一个在iframe中的元素。所以我尝试切换到iframe。

html中的iframe:

<iframe id="id_of_iframe"  data-app-id="com.s-h.ph.pr" class="external-app ng-scope ng-isolate-scope" ng-if="!vm.appDisabled"  ng-attr-sandbox="{{vm.iframeConfig.sandbox ? vm.iframeConfig.sandbox : ''}}" tp-onload="vm.sendCurrentRouteToIFrame()" style="border: none; border-width: 0; height: 100%; width: 100%; display: block;" sandbox="allow-forms allow-popups allow-pointer-lock allow-same-origin allow-scripts allow-top-navigation allow-modals"></iframe> 

使用的代码:

 browser.switchTo().frame('id_of_iframe');

我有以下错误:

 Failed: no such frame
      (Session info: chrome=61.0.3163.100)
      (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c58
10906a),platform=Windows NT 6.1.7601 SP1 x86_64)
  Stack:
    NoSuchFrameError: no such frame
      (Session info: chrome=61.0.3163.100)
      (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c58
10906a),platform=Windows NT 6.1.7601 SP1 x86_64)
        at WebDriverError (C:Usersz002ex9tAppDataRoamingnpmnode_modulespr
otractornode_modulesselenium-webdriverliberror.js:27:5)
        at NoSuchFrameError (C:Usersz002ex9tAppDataRoamingnpmnode_modules
protractornode_modulesselenium-webdriverliberror.js:180:5)
        at Object.checkLegacyResponse (C:Usersz002ex9tAppDataRoamingnpmnod
e_modulesprotractornode_modulesselenium-webdriverliberror.js:505:15)
        at parseHttpResponse (C:Usersz002ex9tAppDataRoamingnpmnode_modules
protractornode_modulesselenium-webdriverlibhttp.js:509:13)
        at doSend.then.response (C:Usersz002ex9tAppDataRoamingnpmnode_modu
lesprotractornode_modulesselenium-webdriverlibhttp.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:109:7)

我正在使用以下版本:"量角器":" 5.1.2","茉莉":" 2.8.0" WebDriver-Manager 12.0.6

任何想法如何解决此问题?

预先感谢。

您可以通过给出

来尝试通过索引
browser.switchTo().frame("index of the frame"); 

这是最简单的,也许我们可以弄清楚这是ID还是其他问题。因为您给出的ID是直截了当的,我看不出它应该失败的原因

WebDriver的driver.switchTo().frame()方法采用三个可能的参数之一:

  1. 数字(iframe的索引)
  2. 名称或ID(如果IFRAME具有ID或名称属性)
  3. webelement(如果需要通过任何自定义逻辑来标识iframe)

示例:

考虑以下IFRAMES在您的DOM中可用。

iframe1:

<iframe name="iframe1" id="iframe_1" class="iframe_1">
</iframe>

iframe2:

<iframe name="iframe2" id="iframe_2" class="iframe_2">
</iframe>

iframe3:

<iframe name="iframe3" id="iframe_3" class="iframe_3">
</iframe>

使用索引:

  • browser.switchTo().frame(0)-这将切换到iframe1
  • browser.switchTo().frame(1)-这将切换到iframe2
  • browser.switchTo().frame(2)-这将切换到iframe3

名称或ID:

  • browser.switchTo().frame("iframe1")browser.switchTo().frame("iframe_1")-这将切换到iframe1
  • browser.switchTo().frame("iframe2")browser.switchTo().frame("iframe_2")-这将切换到iframe2
  • browser.switchTo().frame("iframe3")browser.switchTo().frame("iframe_3")-这将切换到iframe3

WebElement

  • browser.switchTo().frame(element(by.tagName("iframe")).getWebElement())browser.switchTo().frame(element(by.css(".iframe_1")).getWebElement())-这将切换到iframe1
  • browser.switchTo().frame(element(by.css(".iframe_2")).getWebElement())browser.switchTo().frame(element(by.xpath(".//iframe[@id='iframe_2']")).getWebElement())-这将切换到iframe2

如果有框架使用xpath

//iframe[@data-app-id='com.s-h.ph.pr']

但我仍然建议您首先必须使用id

最新更新