如何在Selenium PHP单元中切换到iframe

eqqqjvef  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(152)

我想写一个测试登录一个网站。问题是当我点击登录弹出窗口出现,这是一个iframe。现在我写代码切换窗口。但我切换到不同的iframe。我不知道它来自这里是一个代码片段。我使用 selenium WebDriver写的Facebook。

<?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
 }  
}
?>
slhcrj9b

slhcrj9b1#

要与frame或iframe中的元素交互,请先切换到该frame!

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

// do your stuff here and switch back afterwards

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

请参见wiki。https://github.com/facebook/php-webdriver/wiki/Alert,-窗口选项卡,-框架iframe-和-活动元素#框架iframe

相关问题