首页 > 解决方案 > 如何在 Xpath 中使用带有“类名”的“not”属性?

问题描述

//span[@class='cname']//text()[not(parent::em[@class='multiple']) and not(normalize-space()='')]

我需要text()在这段代码中删除。因为当我使用它时text(),它就变成了一个对象。.text但是,如果我在我的 python 代码末尾键入“ ”,它就会变成 element。

我需要一个元素而不是一个对象。所以我需要text()从代码中重写“”而不进行任何其他更改,或者将其从对象更改为元素。

标签: pythonxpath

解决方案


如果您想选择它的父元素,text()您可以添加/..或添加/parent::*到 XPath 的末尾

//span[@class='cname']//text()[not(parent::em[@class='multiple']) and not(normalize-space()='')]/..

或者

//span[@class='cname']//text()[not(parent::em[@class='multiple']) and not(normalize-space()='')]/parent::*

推荐阅读