单击按钮时生成的iFrame的书写测试。 iFrame名称和ID相似,但是是动态生成的。我之前已经成功使用过“ switchToiFrame”。
<div class="bbbb-frame-container" style="height: 400px; width: 665px; margin-top: -200px; margin-left: -332.5px;">
<div class="user-support-frame-close-container" style="display: block;">
<div class="user-support-frame-close">Close</div>
</div>
<iframe id="poplock_default9636_priv" frameborder="0" name="poplock_default9636_priv" src="blah.blaag.com">
<!DOCTYPE html>
.....................
poplock_defaultNNNN_priv是我想切换到的动态生成的iFrame。
[首先,找到一个具有find methods的iframe。如果它是页面上唯一的iframe(或第一个),请使用find() method
。如果不是,则必须使用findAll()
并依赖顺序(因为没有可搜索的独特属性)。
一旦找到iframe,您就可以使用getAttribute
来获取其名称,然后使用getAttribute
来...好吧,切换到iframe。
如果只需要简单地操作iFrame,请参见switchToIframe
。例如,答案给出了Javascript以获取第一个iFrame,因此您可以对其进行操作:
$page = $this->getSession()->getPage();
$this->getSession()->getDriver()->switchToIFrame('stripe_checkout_app');
$page = $this->getSession()->getPage();
/** @var NodeElement $stripeInputField */
$stripeInputField = $page->findField($field); // where $field can be: 'Email, CVC, Card Number, MM / YY'
$emailInput->setValue($value);
// Switch Back to Main Window
$this->getSession()->getDriver()->switchToIFrame(null);
他们还提供了任意iFrame的jQuery示例:
this question
虽然这些并不是您所要的,但您可以更改它们以找到第n个iFrame,如果您知道生成的iFrame的数量以及想要操作的iFrame,然后根据需要进行操作。
对谁可能有用的方法,我正在使用已知前缀上的CSS选择器查找动态命名的iframe:
window.frames[0].document.body.<your action here>
CSS选择器<iframe id="my_iframe" ...></iframe>
$('#my_iframe').contents().find('html').html();
表示“以...开头”-相反,如果它不是前缀,则可以将// Switch to the payment iframe.
$iframe = $this->getSession()->getPage()->find('css', 'iframe[name^="__privateStripeFrame"]');
$iframe_name = $iframe->getAttribute('name');
$this->getSession()->switchToIFrame($iframe_name);
用作“包含...”。
此示例是要获取Stripe 3D Secure弹出窗口。