Php 在webdriver中呈现HTML字符串或本地HTML文件

Php 在webdriver中呈现HTML字符串或本地HTML文件,php,selenium,selenium-webdriver,webdriver,php-5.6,Php,Selenium,Selenium Webdriver,Webdriver,Php 5.6,我想通过facebook/php webdriver呈现一个本地HTML文件 例如: $host = 'http://phantomjs:8910/wd/hub'; // webdriver server is on another host $driver = RemoteWebDriver::create($this->host, DesiredCapabilities::phantomjs()); $driver->get('file:///tmp/test.html');

我想通过facebook/php webdriver呈现一个本地HTML文件

例如:

$host = 'http://phantomjs:8910/wd/hub'; // webdriver server is on another host
$driver = RemoteWebDriver::create($this->host, DesiredCapabilities::phantomjs());
$driver->get('file:///tmp/test.html'); 
但它无法加载本地文件

如果我可以呈现HTML字符串,那就太好了:

$text = <<<EOT
<html><head><title>Test HTML</title></head><body><div>TEST BODY</div></body></html>
EOT;
$driver = RemoteWebDriver::create($this->host, DesiredCapabilities::phantomjs());
$driver->getHTML($text);   
这些问题的最佳解决方案是什么。

我认为(目前)在任何selenium绑定中都没有一种方法可以让浏览器打开文件(这会给远程驱动程序带来自己的问题),但是javascript可以“欺骗”这种方法

我们的想法是打开任何url,然后通过js
document.write()
用自己的html替换页面的html。以下是基于您的代码的解决方案:

// the target html - in the sample it's just a string var
// in the final version - read it from the file system
$text = <<<EOT
<html><head><title>Test HTML</title></head><body><div>TEST BODY</div></body>
</html>
EOT;    
// the JS we will use to change the html
$js = sprintf("document.write('%s);",$text);

// get the driver
$host = 'http://phantomjs:8910/wd/hub'; // webdriver server is on another host
$driver = RemoteWebDriver::create($this->host, DesiredCapabilities::phantomjs());

// open a generic site, you know is reachable
$driver->get('http://google.com');
// and now, just change the source through JS's document.write()
$driver->executeScript($js);
//目标html-在示例中,它只是一个字符串变量
//在最终版本中-从文件系统中读取它
$text=
// the target html - in the sample it's just a string var
// in the final version - read it from the file system
$text = <<<EOT
<html><head><title>Test HTML</title></head><body><div>TEST BODY</div></body>
</html>
EOT;    
// the JS we will use to change the html
$js = sprintf("document.write('%s);",$text);

// get the driver
$host = 'http://phantomjs:8910/wd/hub'; // webdriver server is on another host
$driver = RemoteWebDriver::create($this->host, DesiredCapabilities::phantomjs());

// open a generic site, you know is reachable
$driver->get('http://google.com');
// and now, just change the source through JS's document.write()
$driver->executeScript($js);