Javascript 使用Nightwatch访问iFrame元素

Javascript 使用Nightwatch访问iFrame元素,javascript,css,dom,iframe,nightwatch.js,Javascript,Css,Dom,Iframe,Nightwatch.js,我正在使用Nightwatch测试iframe中的div是否得到了正确的值 我的html <div class="o-ads__inner" id="leaderboard-gpt"> <div id="google_ads_iframe"> <iframe id="some_id"> #document <html> <head></head>

我正在使用Nightwatch测试iframe中的
div
是否得到了正确的值

我的html

<div class="o-ads__inner" id="leaderboard-gpt">
  <div id="google_ads_iframe">
    <iframe id="some_id">
      #document
        <html>
          <head></head>
          <body>
            <div id="ad-data" creative-id="53134803289">
              <!-- All of the stuff -->
            </div>
          </body>
        </html>
    </iframe>
  </div>
  <iframe><!-- Another iframe (I don't want) --></iframe>
</div>
我从Nightwatch得到的错误是,在等待元素出现5000毫秒时,
超时。
我通过在出现故障的行之前检查,知道它在那里

我已经输入了2
.pause(5000)
s,因为类似的问题表明iframe中的内容加载速度不够快可能是个问题。我不认为这里的情况是这样的,因为我做了一些调试,但我暂时把它们放在那里了

我认为我无法访问
div#ad数据
,因为iframe有它自己的
contentDocument
属性,它包含
(以及随后的
div


如何使用Nightwatch在iframe的
contentDocument
中断言
div
属性的值?

对于第一个iframe
使用
.frame('some_id')

动态id外部iframe访问 下面是一种测试解决此问题的方法,获取属性id并在帧中使用它,帧(0)不提供访问,nightwatch需要iframe id。对于动态生成的id,您可以这样做

module.exports = {
      'Checkout Braintree' : function (client) {
        client
        .url('#enter_your_url_here')
        .waitForElementPresent('[data-e2e="brainTree3Ds"]', 10000)
        .pause(5000)
        .perform(() => {
          client.getAttribute('[data-e2e="brainTree3Ds"] iframe', 'id',(result) => {
            client
              .frame(result.value)
              .setValue('input[name="external.field.password"]', 1234)
              .click('input[type="submit"]');
          });
        })
        .testSuccessPage() // External Command
        .end();
    }

谢谢,在上面的代码中,我并不是真的想在iframe上放一个id,它确实有一个id,但它可能会根据插入的广告而变化,这就是我为什么要执行
.frame(0)
。也就是说,当我尝试使用id时(有时会是正确的),我做了
.frame('some#u id')
来选择它,但一直忽略了一个事实,我在其中放了一个
。然后为了转义iframe,
.frameParent()
你是如何得到没有id的框架的。。iframe不会每次都有相同的id?@Jaimin我的iframe的id不是我设置的,我们不知道它是什么,因为它是由广告服务器提供的。但我们通常都知道这是页面上的第一个iFrame。谢谢您提供的信息。我正在检查谷歌广告是否被渲染或没有使用夜视。已经找到了一些方法。@Jaimin cool,如果你成功了,我很想看看你是如何做到的。检查这个测试用例,你好,Tarandeep。回答很好,但我在运行时遇到一个错误,说是
错误。clickElement()协议操作:未知错误:无法确定加载状态,请参考
。单击('input[type=“submit”]”)零件。你知道为什么会这样吗?
module.exports = {
      'Checkout Braintree' : function (client) {
        client
        .url('#enter_your_url_here')
        .waitForElementPresent('[data-e2e="brainTree3Ds"]', 10000)
        .pause(5000)
        .perform(() => {
          client.getAttribute('[data-e2e="brainTree3Ds"] iframe', 'id',(result) => {
            client
              .frame(result.value)
              .setValue('input[name="external.field.password"]', 1234)
              .click('input[type="submit"]');
          });
        })
        .testSuccessPage() // External Command
        .end();
    }