我必须在没有姓名/id的情况下验证iframe中的文本/消息。经过探索,我找到了添加id/name并使用switchToIFrame(id/name)的解决方案。如何将id/name设置为mink中的节点元素?SetAttribute()不是节点中支持的方法,并且帧不支持setValue()。
<html>
<body>
<div class="div1">
<iframe class="xyz">
<!DOCTYPE html>
<html>
<body>
<div class="frame"></div>
<p>The paragraph1</p>
</body>
</html>
</body>
</html>我的上下文文件是
public function iShouldSeeDescription($arg1)
{
$expected = "The paragraph1";
$iframe=$this->getSession ()->getPage ()->find ( 'xpath', '//div[contains(@class,"div1")]/iframe[contains(@class,"xyz")]');
$iframe->setAttribute('id', 'iframe_id');
$iframeId = $iframe->getAttribute('id');
$this->getSession()->switchToIframe("$iframeId");
$actual = $this->getSession()->getPage()->find('xpath', '//div[@class="frame"]/p[1]');
if ($actual === null) {
throw new \InvalidArgumentException ( sprintf ( 'null:%s', $arg1 ) );
}
if (!($actual === $expected)) {
throw new \InvalidArgumentException ( sprintf ( 'The Acutal description:%s and expected description:%s did not match ', $actual, $expected ) );
}
}发布于 2016-08-01 01:59:04
将id/name设置为iframe的方法是使用javascript。
创建一个像switchToIFrameBySelector($iframeSelector)这样的方法,它等待给定的元素可用,然后您可以执行javascript代码来为您的iframe设置名称/id,并在使用switchToIFrame方法之后。
为了执行js脚本,您可以在try-catch中使用executeScript方法,并在需要时抛出自定义异常。
public function switchToIFrame($iframeSelector){
$function = <<<JS
(function(){
var iframe = document.querySelector("$iframeSelector");
iframe.name = "iframeToSwitchTo";
})()
JS;
try{
$this->getSession()->executeScript($function);
}catch (Exception $e){
print_r($e->getMessage());
throw new \Exception("Element $iframeSelector was NOT found.".PHP_EOL . $e->getMessage());
}
$this->getSession()->getDriver()->switchToIFrame("iframeToSwitchTo");
}
在js中设置元素的id/name的另一种方法是使用:
setAttribute('name', 'iframeToSwitchTo')
发布于 2017-11-24 09:40:46
将数字或for循环用于迭代所需的循环,然后设置数字与交叉原点一起工作
$this->getSession()->getDriver()->switchToIFrame(0);
https://stackoverflow.com/questions/38667172
复制相似问题