我想编写测试以登录站点。问题是当我单击“登录”时,会出现一个 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();
关于php - 如何在 selenium PHP 单元中切换到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456777/