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

标签 php selenium iframe phpunit

我想编写测试以登录站点。问题是当我单击“登录”时,会出现一个 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/

相关文章:

python - 使用 Selenium 在 Firefox 上单击 Javascript 链接

javascript - 检测 &lt;iframe&gt; 父元素何时从隐藏更改为显示

html - 在新标签页的 iframe 中打开 PDF - Google Chrome

php - 具有数组结构的字符串到数组

php - WordPress wp_list_categories,如何突出显示 “All categories”

selenium - 构建 e2e 测试的最佳实践有哪些?

python - 在selenium python中每5秒打印一次移动文本

asp.net - Iframe 内的站点,从父站点继承 CSS

php - Wordpress get_post_meta - 如何检查多个 key ?

php - 在 Wordpress 中切换两种语言