selenium - 使用 ID 使用 Codeception 切换 IFrame

标签 selenium iframe codeception

如何使用 ID 切换到带有 Codeception 的 IFrame?实际上我可以使用 IFrame 的名称,但不能使用 ID -> Codeception SwitchToIFrame

IFrame 示例:

<iframe id="iframeToolbar" src="link" frameborder="0" style="position: fixed; left: 0px; top: 0px; z-index: 998; width: 940px;" scrolling="no" width="100px" height="100%"></iframe>

代码接收示例:
<?php
# switch to iframe
$I->switchToIFrame("another_frame");
# switch to parent page
$I->switchToIFrame();

是否可能是 Codeception <-> Facebook Webdriver 连接问题?

编辑:我按照快速步骤指南重新安装了 Codeception。结果 - 问题仍然相同:Codeception 和 Facebook Webdriver 不想一起工作。 My Codeception Code

accept.suite.yml:
actor: AcceptanceTester
modules:
  enabled:
    - \Helper\Acceptance
    - WebDriver:
            url: https://xxxxxxxxxxxxxx.com
            browser: chrome
            window_size: maximize
            clear_cookies: true

codeception.yml:
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
    - Codeception\Extension\RunFailed
settings:
colors: true
  • 操作系统:Windows 10
  • PHP:7.1.7
  • PHPUnit:6.4.4
  • Selenium 服务器:3.8.1
  • 代码接收:2.3.7
  • Chrome :63.0.3239.108
  • 最佳答案

    我遇到了这个问题,测试了一个包含 Google reCaptcha 的页面...... reCaptcha 在它自己的 iframe 中,并且由于该 iframe 是由 3rd 方代码生成的,我们无法控制它的 name 属性(至少当它是首先生成)。

    我最终做的是运行一个 javascript 片段,它可以根据其他东西找到 iframe,然后给它一个 id,以便 Codeception Webdriver 可以切换到它:

        public function _clickOnCaptcha(AcceptanceTester $I)
    {
        // give the recaptcha iframe a name so codeception webdriver can switch to it
        $recaptcha_frame_name = 'recaptcha-frame';
        $I->executeJS("$('.g-recaptcha iframe').attr('name', '$recaptcha_frame_name')");
        $I->switchToIFrame($recaptcha_frame_name);
        $I->see('not a robot');
        $I->seeElement('.rc-anchor');
        $I->click(['id' => 'recaptcha-anchor']);
        $I->switchToIFrame(); // switch back to main window
    }
    

    我确实控制了包含的元素,所以在这种情况下,它包含在类 g-recaptcha 的元素中。 ...所以我们使用 jquery 来查找该元素内的 iframe:$('.g-recaptcha iframe') ,然后给它一个名称属性:.attr('name', '$recaptcha_frame_name') .

    然后我们可以使用Codeception切换到它并点击验证码复选框: $I->switchToIFrame($recaptcha_frame_name); $I->click(['id' => 'recaptcha-anchor']);
    然后,当我们完成后,切换回主框架,以便我们可以提交表单: $I->switchToIFrame(); // switch back to main window
    注意,我在测试环境中使用了此处指定的 reCaptcha 测试 key ,因此它永远不会真正要求解决验证码。

    https://developers.google.com/recaptcha/docs/faq

    With the following test keys, you will always get No CAPTCHA and all verification requests will pass.

    Site key: 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI Secret key: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe The reCAPTCHA widget will show a warning message to claim that it's only for testing purposes. Please do not use these keys for your production traffic.

    关于selenium - 使用 ID 使用 Codeception 切换 IFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890355/

    相关文章:

    javascript - 如何访问 Protractor 测试的 chromedriver 日志

    java - 如何使用 Selenium 在可点击下一个按钮的情况下逐一解析网页?

    javascript - 相对于整个屏幕在 iframe 内保持固定位置

    firefox - Firefox:滚动包含嵌入YouTube视频的iframe

    php - 使用 Codeception 缩放页面?

    javascript - 从 selenium/protractor javascript 中的 span 元素中提取内部

    javascript - 为什么 javascript 不能改变 iframe 的 dom,但浏览器工具可以?

    cookies - 如何从 Codeception 验收测试中访问变量命名的 cookie?

    php - 在 Codeception 中将调试输出打印到控制台

    css - ruby / Selenium /Watir-Webdriver : "path is not absolute" error for absolute path