gpt4 book ai didi

php - 如何在 selenium PHP 单元中切换到 iframe

转载 作者:行者123 更新时间:2023-12-05 04:16:28 24 4
gpt4 key购买 nike

我想编写测试以登录站点。问题是当我单击“登录”时,会出现一个 iframe 弹出窗口。现在我编写代码在窗口之间切换。但是我切换了不同的iframe。我不知道它来自哪里这是一段代码。我使用 Facebook 编写的 selenium WebDriver。

<?php
class TitleTest extends PHPUnit_Framework_TestCase {

protected $webDriver;
public function setUp()
{
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
$this->webDriver = RemoteWebDriver::create('http://ip-address:4444/wd/hub', $capabilities);
}
protected $url = 'https://mywebsite.net';

public function testAfterLoginBudliTitle()
{
// Sell used Login and account page's title assert
$this->webDriver->get($this->url);
$this->webDriver->manage()->window()->maximize();
$link = $this->webDriver->findElement(WebDriverBy::xpath('/html/body/div[3]/header/div[2]/div/ul/li[2]/a'));
sleep(2);
$link->click(); //click on login
sleep(3);
$iframe = $this->webDriver->findElement(WebDriverBy::tagName('iframe'));

$frameName = $iframe->getAttribute('name');
//Switch the driver to the iframe
$this->webDriver->switchTo()->frame($frameName);
$this->assertContains('demo', $frameName); // Here I get the different iframe name
}
}
?>

最佳答案

为了与框架或 iframe 内的元素交互,请先切换到该框架!

$my_frame = 'id_or_name';
$driver->switchTo()->frame($my_frame);

... do your stuff here and switch back afterwards ...

$driver->switchTo()->defaultContent();

参见维基。 https://github.com/facebook/php-webdriver/wiki/Alert,-Window-Tab,-frame-iframe-and-active-element#frame-iframe

关于php - 如何在 selenium PHP 单元中切换到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456777/

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