gpt4 book ai didi

iframe - Mink/behat iframe 没有 id/name

转载 作者:行者123 更新时间:2023-12-02 03:21:20 25 4
gpt4 key购买 nike

我必须在没有名称/ID 的情况下验证 iframe 内的文本/消息。经过探索,我找到了添加 id/name 并使用 switchToIFrame(id/name) 的解决方案。如何将 id/name 设置为 mink 中的节点元素? NodeElement 中不支持 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 ) );
}

}

最佳答案

您可以使用 javascript 将 id/name 设置为 iframe。

创建一个类似 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')

关于iframe - Mink/behat iframe 没有 id/name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com