关于Selenium WebDriver:在protractor测试中无法找到iframe

not able to find iframe in protractor tests

我正在尝试查找iframe中的元素。因此,我尝试切换到iframe。

HTML中的iframe:

1
<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"></iframex>

使用的代码:

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

我收到以下错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 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:\\Users\\z002ex9t\\AppData\
oaming\
pm\
ode_modules\\pr
otractor\
ode_modules\\selenium-webdriver\\lib\\error.js:27:5)
        at NoSuchFrameError (C:\\Users\\z002ex9t\\AppData\
oaming\
pm\
ode_modules\\
protractor\
ode_modules\\selenium-webdriver\\lib\\error.js:180:5)
        at Object.checkLegacyResponse (C:\\Users\\z002ex9t\\AppData\
oaming\
pm\
od
e_modules\\protractor\
ode_modules\\selenium-webdriver\\lib\\error.js:505:15)
        at parseHttpResponse (C:\\Users\\z002ex9t\\AppData\
oaming\
pm\
ode_modules
\\protractor\
ode_modules\\selenium-webdriver\\lib\\http.js:509:13)
        at doSend.then.response (C:\\Users\\z002ex9t\\AppData\
oaming\
pm\
ode_modu
les\\protractor\
ode_modules\\selenium-webdriver\\lib\\http.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:109:7)

我正在使用以下版本:
"protractor":" 5.1.2 ",
"jasmine":" 2.8.0 "
webdriver-manager 12.0.6

有什么办法解决这个问题吗?

预先感谢。


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

  • 编号(iframe的索引)
  • 名称或ID(如果iframe具有ID或Name属性)
  • WebElement(如果iframe需要通过任何自定义逻辑进行标识)
  • 示例:

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

    iframe1:

    1
    2
    <iframe name="iframe1" id="iframe_1" class="iframe_1">
    </iframex>

    iframe2:

    1
    2
    <iframe name="iframe2" id="iframe_2" class="iframe_2">
    </iframex>

    iframe3:

    1
    2
    <iframe name="iframe3" id="iframe_3" class="iframe_3">
    </iframex>

    使用指数:

    • 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

    您能否通过给出

    来尝试传递索引

    1
    browser.switchTo().frame("index of the frame");

    这是最简单的一个,也许我们可以弄清楚这是id还是其他问题。因为您提供的ID是直接的,并且我看不出它应该失败的原因


    如果有框架,请使用xpath

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

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