php web html文件,php – 在webdriver中呈现HTML字符串或本地html文件

我认为(目前)浏览器的任何selenium绑定都没有办法打开文件(这会给远程驱动程序带来问题),但这可能会被javascript“欺骗”.

这个想法是打开任何网址,然后用你自己的 – 用js document.write()替换页面的html.这是基于您的代码的解决方案:

// the target html - in the sample it's just a string var

// in the final version - read it from the file system

$text = <<Test HTML

TEST BODY

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);

免责声明 – php不是我的力量(事实上,这是我的弱点:D),所以上面的代码示例可能远非完美

几句谨慎的话

> JS使用’char作为字符串边界,因此自然不应该存在该字符/应该在原始源中编码.通过将html传递为an argument可以避免这种情况

>源代码中的换行符将产生一个js,它将引发SyntaxError:执行时出现无效或意外的令牌;因此他们must be stripped

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值