我想在页面上获取所有h2标头的xpath。我正在使用Symfony DomCrawler来完成它,我已经创建了以下解决方法
$h2s = $crawler->filter('h2');
$h2s->each(function($header)
{
$path = [];
foreach($header->parents() as $parent)
{
$path[] = $parent->tagName;
}
$path = '//' . join("/", array_reverse($path));
});
无论如何,它并不完美。生成的xpath不是唯一的。有些div有必要的类,ID等。如何为每个dom元素生成xpath或者像hash这样的其他独特属性?