【问题标题】:DOMxpath query returns nothingDOMxpath 查询不返回任何内容
【发布时间】:2017-05-01 11:38:26
【问题描述】:

我需要在我的网页上加载、修改和输出一些第 3 方iframe。正如建议的那样,我创建了一个中间页面,它只包含 <iframe> 及其 src 属性。

输出页面的php代码如下:

$iframe->loadHTML(file_get_contents("http://example.com/iframe_page.php")); //creating DOM object, see htm content below
$xpathObj= new DOMXPath($iframe);//creating DOMXPath object
foreach ($xpathObj->query('//div[@id="specific_id"]') as $node){ //this query returns nothing
    $node->parentNode->removeChild($node);//i need to remove the div with that id, but there is nothing to remove
}
echo $iframe->saveHTML($iframe->documentElement);//the iframe output works fine

我的iframe 的内容看起来像这样:

<html>
    <head>
    </head>
    <body>
        <div>
            <div>
                <div id="specific_id">
                </div>
            </div>
        </div>
    </body>
</html>

我什至尝试禁用 JS 以查看此 div 是否由前端代码放置在那里,没有 JS 禁用文档的结构看起来完全一样。

【问题讨论】:

  • 你确定你有这个html的内容吗?因为您的代码在上面运行良好..
  • 您的代码正在运行检查此eval.in/785036
  • @SahilGulati 我确定是的,它的结构无论如何......这里还有什么其他的东西可以玩?
  • 你能分享你完整的HTML内容吗,因为你上面的代码在这里工作正常eval.in/785036

标签: php iframe domxpath


【解决方案1】:

答案很简单。您不能使用 PHP DOMDocument 操作 iframe 元素跨域。

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 2011-05-05
    • 2012-04-14
    • 1970-01-01
    • 2021-04-24
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多